<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>Python 网络与并发编程 | SanShui的个人博客</title><meta name="author" content="SanShui"><meta name="copyright" content="SanShui"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="ffffff"><meta name="description" content="Python 网络与并发编程">
<meta property="og:type" content="article">
<meta property="og:title" content="Python 网络与并发编程">
<meta property="og:url" content="https://huaiyuechusan.github.io/archives/984515cf.html">
<meta property="og:site_name" content="SanShui的个人博客">
<meta property="og:description" content="Python 网络与并发编程">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://wallpaper.csun.site/?6">
<meta property="article:published_time" content="2024-01-06T12:16:00.000Z">
<meta property="article:modified_time" content="2024-10-17T11:57:47.423Z">
<meta property="article:author" content="SanShui">
<meta property="article:tag" content="学习技术">
<meta property="article:tag" content="Python">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://wallpaper.csun.site/?6"><link rel="shortcut icon" href="/./img/config_img/%E9%98%B3%E5%85%89%E5%B0%8F%E7%8C%AB.jpg"><link rel="canonical" href="https://huaiyuechusan.github.io/archives/984515cf"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.xml","preload":true,"languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"簡"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: true,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: {"chs_to_cht":"你已切换为繁体","cht_to_chs":"你已切换为简体","day_to_night":"你已切换为深色模式","night_to_day":"你已切换为浅色模式","bgLight":"#49b1f5","bgDark":"#1f1f1f","position":"bottom-left"},
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'Python 网络与并发编程',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2024-10-17 19:57:47'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', 'ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><link rel="stylesheet" href="/css/custom.css" media="defer" onload="this.media='all'"><script src="https://npm.elemecdn.com/echarts@4.9.0/dist/echarts.min.js"></script><!-- hexo injector head_end start --><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Zfour/Butterfly-double-row-display@1.00/cardlistpost.min.css"/>
<style>#recent-posts > .recent-post-item >.recent-post-info > .article-meta-wrap > .tags:before {content:"\A";
  white-space: pre;}#recent-posts > .recent-post-item >.recent-post-info > .article-meta-wrap > .tags > .article-meta__separator{display:none}</style>
<link rel="stylesheet" href="https://npm.elemecdn.com/hexo-butterfly-categories-card@1.0.0/lib/categorybar.css"><link rel="stylesheet" href="/./css/runtime.css" media="print" onload="this.media='all'"><!-- hexo injector head_end end --><meta name="generator" content="Hexo 5.4.2"><link rel="alternate" href="/atom.xml" title="SanShui的个人博客" type="application/atom+xml">
</head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><script>const preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',()=> { preloader.endLoading() })

if (true) {
  document.addEventListener('pjax:send', () => { preloader.initLoading() })
  document.addEventListener('pjax:complete', () => { preloader.endLoading() })
}</script><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/./img/config_img/%E9%98%B3%E5%85%89%E5%B0%8F%E7%8C%AB.jpg" onerror="onerror=null;src='./img/config_img/蓝天.jpg'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">25</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">16</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">11</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/comments/"><i class="fa-fw fas fa-envelope-open"></i><span> 留言板</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 链接</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('http://wallpaper.csun.site/?6')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">SanShui的个人博客</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/comments/"><i class="fa-fw fas fa-envelope-open"></i><span> 留言板</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 链接</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">Python 网络与并发编程</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2024-01-06T12:16:00.000Z" title="发表于 2024-01-06 20:16:00">2024-01-06</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2024-10-17T11:57:47.423Z" title="更新于 2024-10-17 19:57:47">2024-10-17</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/Python/">Python</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">13.8k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>50分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="Python 网络与并发编程"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><p><meta name="referrer" content="no-referrer" /></p>
<h1 id="Python-网络与并发编程"><a href="#Python-网络与并发编程" class="headerlink" title="Python 网络与并发编程"></a>Python 网络与并发编程</h1><h2 id="并发编程"><a href="#并发编程" class="headerlink" title="并发编程"></a>并发编程</h2><h3 id="并发编程介绍"><a href="#并发编程介绍" class="headerlink" title="并发编程介绍"></a>并发编程介绍</h3><h4 id="串行-并行-并发的区别"><a href="#串行-并行-并发的区别" class="headerlink" title="串行-并行-并发的区别"></a>串行-并行-并发的区别</h4><p><img src="https://gitee.com/huaiyuechusan/picture/raw/master/Typora/202312302345214.png" alt="image-20231218211529713"></p>
<ol>
<li>串行(serial)):一个CPU上，按顺序完成多个任务</li>
<li>并行(parallelism):指的是任务数小于等于cpu核数，即任务真的是一起执行的</li>
<li>并发(concurrency):一个CPU采用时间片管理方式，交替的处理多个任务。一般是是任务数多余cu核数，通过操作系统的各种任务调度算法，实现用多个任务“一起”执行（实际上总有一些任务不在执行，因为切换任务的速度相当快，看上去一起执行而已)</li>
</ol>
<h4 id="进程-线程-协程的区别"><a href="#进程-线程-协程的区别" class="headerlink" title="进程-线程-协程的区别"></a>进程-线程-协程的区别</h4><p><img src="https://gitee.com/huaiyuechusan/picture/raw/master/Typora/202312302345216.png" alt="image-20231218212430086"></p>
<p><img src="https://gitee.com/huaiyuechusan/picture/raw/master/Typora/202312302345217.png" alt="image-20231218212524691"></p>
<ol>
<li><strong>线程是程序执行的最小单位</strong>，而==进程==是操作系统==分配资源==的最小单位；</li>
<li>一个进程由一个或多个线程组成，线程是一个进程中代码的不同执行路线；</li>
<li>进程之间相互独立，但同一进程下的各个线程之间共享程序的内存空间（包括代码段、数据集、堆等)及一些进程级的资源（如打开文件和信号），某进程内的线程在其它进程不可见；</li>
<li>调度和切换：线程上下文切换比进程上下文切换要快得多。</li>
</ol>
<blockquote>
<p>进程(Process):拥有自己独立的堆和栈，既不共享堆，也不共享栈，进程由操作系统调度；进程切换需要的资源很最大，效率低</p>
<p>线程(Thread):拥有自己独立的栈和共享的堆，共享堆，不共享栈，标准线程由操作系统调度；线程切换需要的资源一般，效率一般（当然了在不考虑GL的情况下）</p>
<p>协程(coroutine):拥有自己独立的栈和共享的堆，共享堆，不共享栈，协程由程序员在协程的代码里显示调度；协程切换任务资源很小，效率高</p>
</blockquote>
<p><strong>进程是什么？</strong></p>
<p><strong>进程（Process）</strong>是一个具有一定独立功能的程序关于某个数据集合的一次运行活动</p>
<p><strong>线程是什么？</strong></p>
<p><strong>线程（Thread）</strong>是操作系统能够进行运算调度的最小单位。它被包含在进程之中，是进程中的实际运作单位。</p>
<p>==<strong>并发编程解决方案：</strong>==</p>
<p>多任务的实现有3种方式：</p>
<ol>
<li>多进程模式</li>
<li>多线程模式</li>
<li>多进程+多线程模式</li>
</ol>
<p><strong>协程是什么？</strong></p>
<p>协程，Coroutines，也叫作纤程(Fiber)，是一种在线程中，比线程更加轻量级的存在，由程序员自己写程序来管理。</p>
<p>当出现IO阻塞时，CPU一直等待IO返回，处于空转状态。这时候用协程，可以执行其他任务。当IO返回结果后，再回来处理数据。充</p>
<p>分利用了IO等待的时间，提高了效率。</p>
<h4 id="同步和异步通信机制的区别"><a href="#同步和异步通信机制的区别" class="headerlink" title="同步和异步通信机制的区别"></a>同步和异步通信机制的区别</h4><p>同步和异步强调的是消息通信机制 (synchronous communication/asynchronous communication)。</p>
<p>同步(synchronous)：A调用B，等待B返回结果后，A继续执行</p>
<p>异步(asynchronous )：A调用B，A继续执行，不等待B返回结果；B有结果了，通知A，A再做处理。</p>
<p><img src="https://gitee.com/huaiyuechusan/picture/raw/master/Typora/202312302345218.png" alt="image-20231219163757077"></p>
<blockquote>
<p>同步方式通信：</p>
<p>1 高淇买一本书《Python实战笔记》。</p>
<p>2 书店老板说：等三分钟啊，我帮你查查。</p>
<p>3 高淇等一小时</p>
<p>4 老板说，找到书了，发给你</p>
<p>异步方式通信：</p>
<p>1 高淇买一本电子书《Python实战笔记》。</p>
<p>2 书店老板说：我查一下，有结果了告诉你。</p>
<p>3 高淇刷抖音一小时</p>
<p>4 老板说，找到书了，发给你</p>
</blockquote>
<h3 id="线程"><a href="#线程" class="headerlink" title="线程"></a>线程</h3><p><img src="https://gitee.com/huaiyuechusan/picture/raw/master/Typora/202312302345219.png" alt="image-20231219172145633"></p>
<p>线程(Thread)特点：</p>
<p>1 <strong>线程（Thread）</strong>是操作系统能够进行运算调度的最小单位。它被包含在进程之中，是进程中的实际运作单位</p>
<p>2 线程是程序执行的最小单位，而进程是操作系统分配资源的最小单位；</p>
<p>3 一个进程由一个或多个线程组成，线程是一个进程中代码的不同执行路线；</p>
<p>4 拥有自己独立的栈和共享的堆，共享堆，不共享栈，标准线程由操作系统调度；</p>
<p>5 调度和切换：线程上下文切换比进程上下文切换要快得多。</p>
<h4 id="线程的创建方式"><a href="#线程的创建方式" class="headerlink" title="线程的创建方式"></a>线程的创建方式</h4><p>Python的标准库提供了两个模块： <code>_thread</code> 和 <code>threading</code> ， <code>_thread</code> 是低级模块， <code>threading</code> 是高级模块，对 <code>_thread</code> 进行了封装。绝大多数情况下，我们只需要使用 <code>threading</code> 这个高级模块。</p>
<p>线程的创建可以通过分为两种方式：</p>
<ol>
<li>方法包装</li>
<li>类包装</li>
</ol>
<p>线程的执行统一通过 <code>start()</code> 方法</p>
<h5 id="方法包装创建线程"><a href="#方法包装创建线程" class="headerlink" title="方法包装创建线程"></a>方法包装创建线程</h5><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#coding=utf-8</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">方法包装建立线程</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="keyword">from</span> threading <span class="keyword">import</span> Thread</span><br><span class="line"><span class="keyword">from</span> time <span class="keyword">import</span> sleep</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">function1</span>(<span class="params">name</span>):</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">f&quot;线程<span class="subst">&#123;name&#125;</span>, start&quot;</span>)  <span class="comment"># format</span></span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">3</span>):</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">f&quot;线程：<span class="subst">&#123;name&#125;</span>, <span class="subst">&#123;i&#125;</span>&quot;</span>)</span><br><span class="line">        sleep(<span class="number">1</span>)</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">f&quot;线程<span class="subst">&#123;name&#125;</span>, end&quot;</span>)  <span class="comment"># format</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&quot;__main__&quot;</span>:</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;主线程，start&quot;</span>)</span><br><span class="line">    <span class="comment"># 创建线程</span></span><br><span class="line">    t1 = Thread(target=function1, args=(<span class="string">&quot;t1&quot;</span>,))</span><br><span class="line">    t2 = Thread(target=function1, args=(<span class="string">&quot;t2&quot;</span>,))</span><br><span class="line">    <span class="comment"># 启动线程</span></span><br><span class="line">    t1.start()</span><br><span class="line">    t2.start()</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;主线程，end&quot;</span>)</span><br><span class="line"></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">运行结果可能会出现换行问题，是因为多个线程抢夺控制台输出的IO流。</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">主线程，start</span></span><br><span class="line"><span class="string">线程t1, start</span></span><br><span class="line"><span class="string">线程：t1, 0</span></span><br><span class="line"><span class="string">线程t2, start</span></span><br><span class="line"><span class="string">线程：t2, 0</span></span><br><span class="line"><span class="string">主线程，end</span></span><br><span class="line"><span class="string">线程：t2, 1线程：t1, 1</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">线程：t2, 2</span></span><br><span class="line"><span class="string">线程：t1, 2</span></span><br><span class="line"><span class="string">线程t1, end线程t2, end</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span>    </span><br></pre></td></tr></table></figure>
<h5 id="类包装创建线程"><a href="#类包装创建线程" class="headerlink" title="类包装创建线程"></a>类包装创建线程</h5><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> time <span class="keyword">import</span> sleep</span><br><span class="line"><span class="keyword">from</span> threading <span class="keyword">import</span> Thread</span><br><span class="line"></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">类包装创建线程</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">MyThread</span>(<span class="title class_ inherited__">Thread</span>):</span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params">self, name</span>):</span><br><span class="line">        Thread.__init__(self)</span><br><span class="line">        self.name = name</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 重写run方法</span></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">run</span>(<span class="params">self</span>):</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">f&quot;线程<span class="subst">&#123;self.name&#125;</span>, start&quot;</span>)  <span class="comment"># format</span></span><br><span class="line">        <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">3</span>):</span><br><span class="line">            <span class="built_in">print</span>(<span class="string">f&quot;线程：<span class="subst">&#123;self.name&#125;</span>, <span class="subst">&#123;i&#125;</span>&quot;</span>)</span><br><span class="line">            sleep(<span class="number">1</span>)</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">f&quot;线程<span class="subst">&#123;self.name&#125;</span>, end&quot;</span>)  <span class="comment"># format</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&#x27;__main__&#x27;</span>:</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;主线程，start&quot;</span>)</span><br><span class="line">    <span class="comment"># 创建线程</span></span><br><span class="line">    t1 = MyThread(<span class="string">&quot;t1&quot;</span>)</span><br><span class="line">    t2 = MyThread(<span class="string">&quot;t2&quot;</span>)</span><br><span class="line">    <span class="comment"># 启动线程</span></span><br><span class="line">    t1.start()</span><br><span class="line">    t2.start()</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;主线程，end&quot;</span>)</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h4 id="join-和守护线程"><a href="#join-和守护线程" class="headerlink" title="join()和守护线程"></a>join()和守护线程</h4><p><strong>join()</strong></p>
<p>之前的代码，主线程不会等待子线程结束。</p>
<p>如果需要==等待子线程结束后，再结束主线程==，可使用join()方法。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> threading <span class="keyword">import</span> Thread</span><br><span class="line"><span class="keyword">from</span> time <span class="keyword">import</span> sleep</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">function1</span>(<span class="params">name</span>):</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">f&quot;线程<span class="subst">&#123;name&#125;</span>, start&quot;</span>)  <span class="comment"># format</span></span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">3</span>):</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">f&quot;线程：<span class="subst">&#123;name&#125;</span>, <span class="subst">&#123;i&#125;</span>&quot;</span>)</span><br><span class="line">        sleep(<span class="number">1</span>)</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">f&quot;线程<span class="subst">&#123;name&#125;</span>, end&quot;</span>)  <span class="comment"># format</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&quot;__main__&quot;</span>:</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;主线程，start&quot;</span>)</span><br><span class="line">    <span class="comment"># 创建线程</span></span><br><span class="line">    t1 = Thread(target=function1, args=(<span class="string">&quot;t1&quot;</span>,))</span><br><span class="line">    t2 = Thread(target=function1, args=(<span class="string">&quot;t2&quot;</span>,))</span><br><span class="line">    <span class="comment"># 启动线程</span></span><br><span class="line">    t1.start()</span><br><span class="line">    t2.start()</span><br><span class="line">    <span class="comment"># 主线程会等待t1,t2结束后，再往下执行</span></span><br><span class="line">    t1.join()</span><br><span class="line">    t2.join()</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;主线程，end&quot;</span>)</span><br></pre></td></tr></table></figure>
<p><strong>守护线程</strong></p>
<p>在行为上还有一种叫守护线程，主要的特征是它的生命周期。==主线程死亡，它也就随之死亡==。在python中，线程通过 <code>setDaemon(True|False)</code>来设置是否为守护线程。</p>
<p>守护线程的作用：</p>
<p>守护线程作用是为其他线程提供便利服务，守护线程最典型的应用就是 GC (垃圾收集器)。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> time <span class="keyword">import</span> sleep</span><br><span class="line"><span class="keyword">from</span> threading <span class="keyword">import</span> Thread</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">MyThread</span>(<span class="title class_ inherited__">Thread</span>):</span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params">self, name</span>):</span><br><span class="line">        Thread.__init__(self)</span><br><span class="line">        self.name = name</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 重写run方法</span></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">run</span>(<span class="params">self</span>):</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">f&quot;线程<span class="subst">&#123;self.name&#125;</span>, start&quot;</span>)  <span class="comment"># format</span></span><br><span class="line">        <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">3</span>):</span><br><span class="line">            <span class="built_in">print</span>(<span class="string">f&quot;线程：<span class="subst">&#123;self.name&#125;</span>, <span class="subst">&#123;i&#125;</span>&quot;</span>)</span><br><span class="line">            sleep(<span class="number">1</span>)</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">f&quot;线程<span class="subst">&#123;self.name&#125;</span>, end&quot;</span>)  <span class="comment"># format</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&#x27;__main__&#x27;</span>:</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;主线程，start&quot;</span>)</span><br><span class="line">    <span class="comment"># 创建线程(类的方式)</span></span><br><span class="line">    t1 = MyThread(<span class="string">&quot;t1&quot;</span>)</span><br><span class="line">    <span class="comment"># t1设置为守护线程</span></span><br><span class="line">    t1.setDaemon(<span class="literal">True</span>)</span><br><span class="line">    <span class="comment"># 启动线程</span></span><br><span class="line">    t1.start()</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;主线程，end&quot;</span>)</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h4 id="全局解释器锁GIL问题"><a href="#全局解释器锁GIL问题" class="headerlink" title="全局解释器锁GIL问题"></a>全局解释器锁GIL问题</h4><p><img src="https://gitee.com/huaiyuechusan/picture/raw/master/Typora/202312302345220.png" alt="image-20231220203021256"></p>
<p>在python中，无论你有多少核，在Cpython解释器中永远都是假象。无论你是4核，8核，还是16核…….不好意思，==同一时间执行的线程只有一个线程==，它就是这个样子的。这个是python的一个开发时候，设计的一个缺陷，所以说python中的线程是“含有水分的线程”。</p>
<p><strong>Python GIL(Global Interpreter Lock)</strong></p>
<p>Python代码的执行由Python 虚拟机(也叫解释器主循环，CPython版本)来控制，Python 在设计之初就考虑到要在解释器的主循环中，同时只有一个线程在执行，即在任意时刻，只有一个线程在解释器中运行。对Python 虚拟机的访问由全局解释器锁（GIL）来控制，正是这个锁能保证同一时刻只有一个线程在运行。</p>
<blockquote>
<p>:warning:GIL并不是Python的特性，它是在实现Python解析器(CPython)时所引入的一个概念,同样一段代码可以通过CPython，PyPy，Psyco等不同的Python执行环境来执行,就没有GIL的问题。然而因为CPython是大部分环境下默认的Python执行环境。所以在很多人的概念里CPython就是Python，也就想当然的把GIL归结为Python语言的缺陷。</p>
</blockquote>
<h4 id="线程同步和互斥锁资源冲突案例"><a href="#线程同步和互斥锁资源冲突案例" class="headerlink" title="线程同步和互斥锁资源冲突案例"></a>线程同步和互斥锁资源冲突案例</h4><p><strong>线程同步的概念</strong></p>
<p>处理多线程问题时，多个线程访问同一个对象，并且某些线程还想修改这个对象。 这时候，我们就需要用到“线程同步”。 线程同步其实就是一种==等待机制==，多个需要同时访问此对象的线程进入这个对象的等待池形成队列，等待前面的线程使用完毕后，下一个线程再使用。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># coding=utf-8</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">未使用线程同步和互斥锁的情况</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="keyword">from</span> threading <span class="keyword">import</span> Thread</span><br><span class="line"><span class="keyword">from</span> time <span class="keyword">import</span> sleep</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Account</span>:</span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params">self, money, name</span>):</span><br><span class="line">        self.money = money</span><br><span class="line">        self.name = name</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 模拟提款操作</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Drawing</span>(<span class="title class_ inherited__">Thread</span>):</span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params">self, drawingNum, account</span>):</span><br><span class="line">        Thread.__init__(self)</span><br><span class="line">        self.drawingNum = drawingNum</span><br><span class="line">        self.account = account</span><br><span class="line">        self.expenseTotal = <span class="number">0</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">run</span>(<span class="params">self</span>):</span><br><span class="line">        <span class="keyword">if</span> self.account.money &lt; self.drawingNum:</span><br><span class="line">            <span class="keyword">return</span></span><br><span class="line">        sleep(<span class="number">1</span>)  <span class="comment"># 判断完后阻塞</span></span><br><span class="line">        self.account.money -= self.drawingNum</span><br><span class="line">        self.expenseTotal += self.drawingNum</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">f&quot;账户<span class="subst">&#123;self.account.name&#125;</span>，余额<span class="subst">&#123;self.account.money&#125;</span>&quot;</span>)</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">f&quot;账户<span class="subst">&#123;self.account.name&#125;</span>，总共取了<span class="subst">&#123;self.expenseTotal&#125;</span>&quot;</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&#x27;__main__&#x27;</span>:</span><br><span class="line">    a1 = Account(<span class="number">100</span>, <span class="string">&quot;gaoqi&quot;</span>)</span><br><span class="line">    draw1 = Drawing(<span class="number">80</span>, a1)  <span class="comment"># 定义取钱线程对象；</span></span><br><span class="line">    draw2 = Drawing(<span class="number">80</span>, a1)  <span class="comment"># 定义取钱线程对象；</span></span><br><span class="line">    draw1.start()  <span class="comment"># 你取钱</span></span><br><span class="line">    draw2.start()  <span class="comment"># 你老婆取钱</span></span><br><span class="line"></span><br><span class="line">    </span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">账户gaoqi，余额20账户gaoqi，余额-60</span></span><br><span class="line"><span class="string">账户gaoqi，总共取了80</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">账户gaoqi，总共取了80</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br></pre></td></tr></table></figure>
<h4 id="互斥锁典型案例"><a href="#互斥锁典型案例" class="headerlink" title="互斥锁典型案例"></a>互斥锁典型案例</h4><p>我们可以通过“<strong>锁机制</strong>”来实现线程同步问题，锁机制有如下几个要点：</p>
<ol>
<li>必须使用同一个锁对象</li>
<li>互斥锁的作用就是保证同一时刻只能有一个线程去操作共享数据，保证共享数据不会出现错误问题</li>
<li>使用互斥锁的好处确保某段关键代码只能由一个线程从头到尾完整地去执行</li>
<li>使用互斥锁会影响代码的执行效率</li>
<li>同时持有多把锁，容易出现死锁的情况</li>
</ol>
<p><strong>互斥锁是什么？</strong></p>
<p>互斥锁: 对共享数据进行锁定，保证同一时刻只能有一个线程去操作。</p>
<p>注意: 互斥锁是<strong>==多个线程一起去抢==</strong>，抢到锁的线程先执行，没有抢到锁的线程需要等待，等互斥锁使用完释放后，其它等待的线程再去抢这个锁</p>
<p><code>threading</code> 模块中定义了 <code>Lock</code> 变量，这个变量本质上是一个函数，通过调用这个函数可以获取一把互斥锁。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># coding=utf-8</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">互斥锁典型案例</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="keyword">from</span> threading <span class="keyword">import</span> Thread, Lock</span><br><span class="line"><span class="keyword">from</span> time <span class="keyword">import</span> sleep</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Account</span>:</span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params">self, money, name</span>):</span><br><span class="line">        self.money = money</span><br><span class="line">        self.name = name</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 模拟提款操作</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Drawing</span>(<span class="title class_ inherited__">Thread</span>):</span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params">self, drawingNum, account</span>):</span><br><span class="line">        Thread.__init__(self)</span><br><span class="line">        self.drawingNum = drawingNum</span><br><span class="line">        self.account = account</span><br><span class="line">        self.expenseTotal = <span class="number">0</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">run</span>(<span class="params">self</span>):</span><br><span class="line">        lock1.acquire()</span><br><span class="line">        <span class="keyword">if</span> self.account.money &lt; self.drawingNum:</span><br><span class="line">            <span class="built_in">print</span>(<span class="string">&quot;账户余额不足&quot;</span>)</span><br><span class="line">            <span class="keyword">return</span></span><br><span class="line">        sleep(<span class="number">1</span>)  <span class="comment"># 判断完后阻塞</span></span><br><span class="line">        self.account.money -= self.drawingNum</span><br><span class="line">        self.expenseTotal += self.drawingNum</span><br><span class="line">        lock1.release()</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">f&quot;账户<span class="subst">&#123;self.account.name&#125;</span>，余额<span class="subst">&#123;self.account.money&#125;</span>&quot;</span>)</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">f&quot;账户<span class="subst">&#123;self.account.name&#125;</span>，总共取了<span class="subst">&#123;self.expenseTotal&#125;</span>&quot;</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&#x27;__main__&#x27;</span>:</span><br><span class="line">    a1 = Account(<span class="number">100</span>, <span class="string">&quot;gaoqi&quot;</span>)</span><br><span class="line">    lock1 = Lock()</span><br><span class="line">    draw1 = Drawing(<span class="number">80</span>, a1)  <span class="comment"># 定义取钱线程对象；</span></span><br><span class="line">    draw2 = Drawing(<span class="number">80</span>, a1)  <span class="comment"># 定义取钱线程对象；</span></span><br><span class="line">    draw1.start()  <span class="comment"># 你取钱</span></span><br><span class="line">    draw2.start()  <span class="comment"># 你老婆取钱</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h4 id="死锁问题和解决方案"><a href="#死锁问题和解决方案" class="headerlink" title="死锁问题和解决方案"></a>死锁问题和解决方案</h4><p><strong>死锁</strong></p>
<p>在多线程程序中，死锁问题很大一部分是由于一个线程同时获取多个锁造成的。</p>
<p><strong>死锁的解决方法</strong></p>
<p>死锁是由于“同步块需要同时持有多个锁造成”的，要解决这个问题，思路很简单，就是：同一个代码块，不要同时持有两个对象锁。</p>
<h4 id="信号量"><a href="#信号量" class="headerlink" title="信号量"></a>信号量</h4><p>互斥锁使用后，一个资源同时只有一个线程访问。如果某个资源，我们同时想让N个(定数值）线程访问？这时候，可以使用信号量。信号量控制同时访问资源的数量。信号量和锁相似，锁同一时间只允许一个对象(进程)通过，信号量同一时间允许多个对象(进程)通过。</p>
<p><strong>应用场景</strong></p>
<ol>
<li>在读写文件的时候，一般只能只有一个线程在写，而读可以有多个线程同时进行，如果需要限制同时读文件的线程个数，这时候就可以用到信号量了（如果用互斥锁，就是限制同一时刻只能有一个线程读取文件）。</li>
<li>在做爬虫抓取数据时。</li>
</ol>
<p><strong>底层原理</strong></p>
<p>信号量底层就是一个内置的计数器。每当资源获取时（调用acquire）计数器-1，资源释放时（调用release）计数器+1。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># coding=utf-8</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">信号量的使用案例</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="keyword">from</span> threading <span class="keyword">import</span> Thread, Lock</span><br><span class="line"><span class="keyword">from</span> time <span class="keyword">import</span> sleep</span><br><span class="line"><span class="keyword">from</span> multiprocessing <span class="keyword">import</span> Semaphore</span><br><span class="line"></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">一个房间一次只允许两个人通过</span></span><br><span class="line"><span class="string">若不使用信号量，会造成所有人都进入这个房子</span></span><br><span class="line"><span class="string">若只允许一人通过可以用锁-Lock()</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">home</span>(<span class="params">name, se</span>):</span><br><span class="line">    se.acquire()</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">f&quot;<span class="subst">&#123;name&#125;</span>进入房间&quot;</span>)</span><br><span class="line">    sleep(<span class="number">3</span>)</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">f&quot;***<span class="subst">&#123;name&#125;</span>走出房间&quot;</span>)</span><br><span class="line">    se.release()</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&#x27;__main__&#x27;</span>:</span><br><span class="line">    se = Semaphore(<span class="number">2</span>)  <span class="comment"># 信号量对象</span></span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">7</span>):</span><br><span class="line">        t = Thread(target=home, args=(<span class="string">f&quot;tom<span class="subst">&#123;i&#125;</span>&quot;</span>, se))</span><br><span class="line">        t.start()</span><br><span class="line">        </span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">tom0进入房间</span></span><br><span class="line"><span class="string">tom1进入房间</span></span><br><span class="line"><span class="string">***tom0走出房间***tom1走出房间</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">tom3进入房间</span></span><br><span class="line"><span class="string">tom2进入房间</span></span><br><span class="line"><span class="string">***tom3走出房间***tom2走出房间</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">tom4进入房间</span></span><br><span class="line"><span class="string">tom5进入房间</span></span><br><span class="line"><span class="string">***tom5走出房间</span></span><br><span class="line"><span class="string">***tom4走出房间</span></span><br><span class="line"><span class="string">tom6进入房间</span></span><br><span class="line"><span class="string">***tom6走出房间</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br></pre></td></tr></table></figure>
<h4 id="事件Event对象"><a href="#事件Event对象" class="headerlink" title="事件Event对象"></a>事件Event对象</h4><p>事件Event主要用于唤醒正在阻塞等待状态的线程。</p>
<blockquote>
<p><strong>原理</strong></p>
<p>Event 对象包含一个可由线程设置的信号标志，它允许线程等待某些事件的发生。在初始情况下，event 对象中的信号标志被设置假。如果有线程等待一个 event 对象，而这个 event 对象的标志为假，那么这个线程将会被一直阻塞直至该标志为真。一个线程如果将一个 event 对象的信号标志设置为真，它将唤醒所有等待个 event 对象的线程。如果一个线程等待一个已经被设置为真的 event 对象，那么它将忽略这个事件，继续执行</p>
</blockquote>
<p><code>Event()</code> 可以创建一个事件管理标志，该标志（event）默认为False，event对象主要有四种方法可以调用：</p>
<div class="table-container">
<table>
<thead>
<tr>
<th><strong>方法名</strong></th>
<th><strong>说明</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td><code>event.wait(timeout=None)</code></td>
<td>调用该方法的线程会被阻塞，如果设置了timeout参数，超时后，线程会停止阻塞继续执行</td>
</tr>
<tr>
<td><code>event.set()</code></td>
<td>将event的标志设置为True，调用wait方法的所有线程将被唤醒</td>
</tr>
<tr>
<td><code>event.clear()</code></td>
<td>将event的标志设置为False，调用wait方法的所有线程将被阻塞</td>
</tr>
<tr>
<td><code>event.is_set()</code></td>
<td>判断event的标志是否为True</td>
</tr>
</tbody>
</table>
</div>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># coding:utf-8</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">小伙伴们，围着吃火锅，当菜上齐了，请客的主人说：开吃！</span></span><br><span class="line"><span class="string">于是小伙伴一起动筷子，这种场景如何实现</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="keyword">import</span> threading</span><br><span class="line"><span class="keyword">import</span> time</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">chihuoguo</span>(<span class="params">name</span>):</span><br><span class="line">    <span class="comment"># 等待事件，进入等待阻塞状态</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">f&#x27;<span class="subst">&#123;name&#125;</span>已经启动&#x27;</span>)</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">f&#x27;小伙伴<span class="subst">&#123;name&#125;</span>已经进入就餐状态！&#x27;</span>)</span><br><span class="line">    time.sleep(<span class="number">1</span>)</span><br><span class="line">    event.wait()</span><br><span class="line">    <span class="comment"># 收到事件后进入运行状态</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">f&#x27;<span class="subst">&#123;name&#125;</span>收到通知了.&#x27;</span>)</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">f&#x27;小伙伴<span class="subst">&#123;name&#125;</span>开始吃咯！&#x27;</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&#x27;__main__&#x27;</span>:</span><br><span class="line">    event = threading.Event()</span><br><span class="line">    <span class="comment"># 创建新线程</span></span><br><span class="line">    thread1 = threading.Thread(target=chihuoguo, args=(<span class="string">&quot;tom&quot;</span>,))</span><br><span class="line">    thread2 = threading.Thread(target=chihuoguo, args=(<span class="string">&quot;cherry&quot;</span>,))</span><br><span class="line">    <span class="comment"># 开启线程</span></span><br><span class="line">    thread1.start()</span><br><span class="line">    thread2.start()</span><br><span class="line">    time.sleep(<span class="number">10</span>)</span><br><span class="line">    <span class="comment"># 发送事件通知</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&#x27;----&gt;&gt;&gt;主线程通知小伙伴开吃咯！&#x27;</span>)</span><br><span class="line">    event.<span class="built_in">set</span>()</span><br><span class="line"></span><br><span class="line">    </span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">tom已经启动</span></span><br><span class="line"><span class="string">小伙伴tom已经进入就餐状态！</span></span><br><span class="line"><span class="string">cherry已经启动</span></span><br><span class="line"><span class="string">小伙伴cherry已经进入就餐状态！</span></span><br><span class="line"><span class="string">----&gt;&gt;&gt;主线程通知小伙伴开吃咯！</span></span><br><span class="line"><span class="string">tom收到通知了.</span></span><br><span class="line"><span class="string">小伙伴tom开始吃咯！</span></span><br><span class="line"><span class="string">cherry收到通知了.</span></span><br><span class="line"><span class="string">小伙伴cherry开始吃咯！</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br></pre></td></tr></table></figure>
<h4 id="生产者-消费者模式"><a href="#生产者-消费者模式" class="headerlink" title="生产者-消费者模式"></a>生产者-消费者模式</h4><p><img src="https://gitee.com/huaiyuechusan/picture/raw/master/Typora/202312302345221.png" alt="image-20231221222417350"></p>
<p>多线程环境下，我们经常需要<strong>多个线程的并发和协作</strong>。这个时候，就需要了解一个重要的多线程并发协作模型“生产者/消费者模式”。</p>
<p><img src="https://gitee.com/huaiyuechusan/picture/raw/master/Typora/202312302345222.png" alt="image-20231221222800905"></p>
<p><strong>什么是生产者？</strong></p>
<p>生产者指的是负责生产数据的模块（这里模块可能是：方法、对象、线程、进程）。</p>
<p><strong>什么是消费者？</strong></p>
<p>消费者指的是负责处理数据的模块（这里模块可能是：方法、对象、线程、进程）</p>
<p><strong>什么是缓冲区？</strong></p>
<p>消费者不能直接使用生产者的数据，它们之间有个“缓冲区”。生产者将生产好的数据放入“缓冲区”，消费者从“缓冲区”拿要处理的数据。</p>
<blockquote>
<p>缓冲区是实现并发的核心，缓冲区的设置有3个好处：</p>
<ol>
<li><p><strong>实现线程的并发协作</strong></p>
<p>有了缓冲区以后，生产者线程只需要往缓冲区里面放置数据，而不需要管消费者消费的情况；同样，消费者只需要从缓冲区拿数据处理即可，也不需要管生产者生产的情况。 这样，就从逻辑上实现了“生产者线程”和“消费者线程”的分离。</p>
</li>
<li><p><strong>解耦了生产者和消费者</strong></p>
<p>生产者不需要和消费者直接打交道。</p>
</li>
<li><p><strong>解决忙闲不均，提高效率</strong></p>
</li>
</ol>
<p>生产者生产数据慢时，缓冲区仍有数据，不影响消费者消费；消费者处理数据慢时，生产者仍然可以继续往缓冲区里面放置数据。</p>
</blockquote>
<p><strong>缓冲区和queue对象</strong></p>
<p>从一个线程向另一个线程发送数据最安全的方式可能就是使用queue 库中的队列了。创建一个被多个线程共享的 Queue 对象，这些线程通过使用 <code>put()</code> 和 <code>get()</code> 操作来向队列中添加或者删除元素。Queue 对象已经包含了必要的锁，所以你可以通过它在多个线程间多安全地共享数据。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># coding=utf-8</span></span><br><span class="line"><span class="keyword">from</span> queue <span class="keyword">import</span> Queue</span><br><span class="line"><span class="keyword">from</span> threading <span class="keyword">import</span> Thread</span><br><span class="line"><span class="keyword">from</span> time <span class="keyword">import</span> sleep</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">producer</span>():</span><br><span class="line">    num = <span class="number">1</span></span><br><span class="line">    <span class="keyword">while</span> <span class="literal">True</span>:</span><br><span class="line">        <span class="keyword">if</span> queue.qsize() &lt; <span class="number">5</span>:</span><br><span class="line">            <span class="built_in">print</span>(<span class="string">f&quot;生产<span class="subst">&#123;num&#125;</span>号，大馒头&quot;</span>)</span><br><span class="line">            queue.put(<span class="string">f&quot;大馒头: <span class="subst">&#123;num&#125;</span>号&quot;</span>)</span><br><span class="line">            num += <span class="number">1</span></span><br><span class="line">        <span class="keyword">else</span>:</span><br><span class="line">            <span class="built_in">print</span>(<span class="string">&quot;馒头框满了，等待来人消费&quot;</span>)</span><br><span class="line">        sleep(<span class="number">1</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">consumer</span>():</span><br><span class="line">    <span class="keyword">while</span> <span class="literal">True</span>:</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">f&quot;获取馒头：<span class="subst">&#123;queue.get()&#125;</span>&quot;</span>)</span><br><span class="line">        sleep(<span class="number">1</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&#x27;__main__&#x27;</span>:</span><br><span class="line">    queue = Queue()</span><br><span class="line">    t1 = Thread(target=producer)</span><br><span class="line">    t2 = Thread(target=consumer)</span><br><span class="line">    t1.start()</span><br><span class="line">    t2.start()</span><br><span class="line">    </span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">生产1号，大馒头</span></span><br><span class="line"><span class="string">获取馒头：大馒头: 1号</span></span><br><span class="line"><span class="string">生产2号，大馒头</span></span><br><span class="line"><span class="string">获取馒头：大馒头: 2号</span></span><br><span class="line"><span class="string">生产3号，大馒头</span></span><br><span class="line"><span class="string">获取馒头：大馒头: 3号</span></span><br><span class="line"><span class="string">生产4号，大馒头</span></span><br><span class="line"><span class="string">获取馒头：大馒头: 4号</span></span><br><span class="line"><span class="string">生产5号，大馒头</span></span><br><span class="line"><span class="string">获取馒头：大馒头: 5号</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br></pre></td></tr></table></figure>
<h3 id="进程"><a href="#进程" class="headerlink" title="进程"></a>进程</h3><p><img src="https://gitee.com/huaiyuechusan/picture/raw/master/Typora/202312302345223.png" alt="image-20231221223750713"></p>
<p>进程(Process)：==拥有自己独立的堆和栈，既不共享堆，也不共享栈==，进程由操作系统调度；进程切换需要的资源很最大，效率低。</p>
<p>对于操作系统来说，一个任务就是一个进程（Process），比如打开一个浏览器就是启动一个浏览器进程，就启动了一个记事本进程，打开两个记事本就启动了两个记事本进程，打开一个Word就启动了一个Word进程。</p>
<p><strong>进程的优缺点</strong></p>
<p>进程的<strong>优点</strong>：</p>
<ol>
<li>可以使用计算机多核，进行任务的并行执行，提高执行效率</li>
<li>运行不受其他进程影响，创建方便</li>
<li>空间独立，数据安全</li>
</ol>
<p>进程的<strong>缺点</strong>：</p>
<p>进程的创建和删除消耗的系统资源较多</p>
<p><strong>进程的创建方式(方法模式)</strong></p>
<p>Python的标准库提供了个模块： <code>multiprocessing</code></p>
<p>进程的创建可以通过分为两种方式：</p>
<ol>
<li>方法包装</li>
<li>类包装</li>
</ol>
<p>创建进程后，使用start()启动进程</p>
<h4 id="方法模式创建进程"><a href="#方法模式创建进程" class="headerlink" title="方法模式创建进程"></a>方法模式创建进程</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># coding=utf-8</span></span><br><span class="line"><span class="comment"># 方法包装-多进程实现</span></span><br><span class="line"><span class="keyword">from</span> multiprocessing <span class="keyword">import</span> Process</span><br><span class="line"><span class="keyword">from</span> time <span class="keyword">import</span> sleep</span><br><span class="line"><span class="keyword">import</span> os</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">func1</span>(<span class="params">name</span>):</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;当前进程ID：&quot;</span>, os.getpid())</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;父进程ID：&quot;</span>, os.getppid())</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">f&quot;Process:<span class="subst">&#123;name&#125;</span> start&quot;</span>)</span><br><span class="line">    sleep(<span class="number">3</span>)</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">f&quot;Process:<span class="subst">&#123;name&#125;</span> end&quot;</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br><span class="line"><span class="string">这是一个关于windows上多进程实现的bug。</span></span><br><span class="line"><span class="string">在windows上，子进程会自动import启动它的这个文件，而在import的时候是会自动执行这些语句的。</span></span><br><span class="line"><span class="string">如果不加__main__限制的话，就会无限递归创建子进程，进而报错。</span></span><br><span class="line"><span class="string">于是import的时候使用 __name__ ==&quot;__main__&quot; 保护起来就可以了。</span></span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&#x27;__main__&#x27;</span>:</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;当前进程ID：&quot;</span>, os.getpid())</span><br><span class="line">    <span class="comment"># 创建进程</span></span><br><span class="line">    p1 = Process(target=func1, args=(<span class="string">&#x27;p1&#x27;</span>,))</span><br><span class="line">    p2 = Process(target=func1, args=(<span class="string">&#x27;p2&#x27;</span>,))</span><br><span class="line">    p1.start()</span><br><span class="line">    p2.start()</span><br><span class="line"></span><br><span class="line">    </span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">当前进程ID： 8132</span></span><br><span class="line"><span class="string">当前进程ID： 2100</span></span><br><span class="line"><span class="string">当前进程ID： 4468</span></span><br><span class="line"><span class="string">父进程ID： 8132</span></span><br><span class="line"><span class="string">Process:p1 start</span></span><br><span class="line"><span class="string">父进程ID： 8132</span></span><br><span class="line"><span class="string">Process:p2 start</span></span><br><span class="line"><span class="string">Process:p1 end</span></span><br><span class="line"><span class="string">Process:p2 end</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br></pre></td></tr></table></figure>
<h4 id="windows-多进程的一个bug"><a href="#windows-多进程的一个bug" class="headerlink" title="windows:多进程的一个bug"></a>windows:多进程的一个bug</h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">这是一个关于windows上多进程实现的bug。</span><br><span class="line">在windows上，子进程会自动import启动它的这个文件，而在import的时候是会自动执行这些语句的。</span><br><span class="line">如果不加__main__限制的话，就会无限递归创建子进程，进而报错。</span><br><span class="line">于是import的时候使用 __name__ ==&quot;__main__&quot; 保护起来就可以了。</span><br></pre></td></tr></table></figure>
<h4 id="类模式创建进程"><a href="#类模式创建进程" class="headerlink" title="类模式创建进程"></a>类模式创建进程</h4><p>和使用Thread 类创建子线程的方式非常类似，使用 Process 类创建实例化对象，其本质是调用该类的构造方法创建新进程。Process类的构造方法格式如下：</p>
<p><code>def __init__(self,group=None,target=None,name=None,args=(),kwargs=&#123;&#125;)</code></p>
<p>其中，各个参数的含义为：</p>
<p><code>group</code> ：该参数未进行实现，不需要传参；</p>
<p><code>target</code> ：为新建进程指定执行任务，也就是指定一个函数；</p>
<p><code>name</code> ：为新建进程设置名称；</p>
<p><code>args</code> ：为 target 参数指定的参数传递非关键字参数；</p>
<p><code>kwargs</code> ：为 target 参数指定的参数传递关键字参数。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># coding=utf-8</span></span><br><span class="line"><span class="keyword">from</span> multiprocessing <span class="keyword">import</span> Process</span><br><span class="line"><span class="keyword">from</span> time <span class="keyword">import</span> sleep</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">MyProcess</span>(<span class="title class_ inherited__">Process</span>):</span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params">self, name</span>):</span><br><span class="line">        Process.__init__(self)</span><br><span class="line">        self.name = name</span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">run</span>(<span class="params">self</span>):</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">f&quot;process:<span class="subst">&#123;self.name&#125;</span>,start&quot;</span>)</span><br><span class="line">        sleep(<span class="number">3</span>)</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">f&quot;process:<span class="subst">&#123;self.name&#125;</span>,end&quot;</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&#x27;__main__&#x27;</span>:</span><br><span class="line">    <span class="comment"># 创建进程</span></span><br><span class="line">    p1 = MyProcess(<span class="string">&quot;p1&quot;</span>)</span><br><span class="line">    p1.start()</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h4 id="Queue实现进程通信"><a href="#Queue实现进程通信" class="headerlink" title="Queue实现进程通信"></a>Queue实现进程通信</h4><p><img src="https://gitee.com/huaiyuechusan/picture/raw/master/Typora/202312302345224.png" alt="image-20231230153859622"></p>
<p>前面讲解了使用 <code>Queue</code> 模块中的 <code>Queue</code> 类实现==线程==间通信，但要实现进程间通信，需要使用 <code>multiprocessing</code> 模块中的 <code>Queue</code> 类。简单的理解 <code>Queue</code> 实现==进程==间通信的方式，就是使用了操作系统给开辟的一个队列空间，各个进程可以把数据放到该队列中，当然也可以从队列中把自己需要的信息取走。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># coding=utf-8</span></span><br><span class="line"><span class="keyword">from</span> multiprocessing <span class="keyword">import</span> Process, Queue</span><br><span class="line"><span class="keyword">from</span> time <span class="keyword">import</span> sleep</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">MyProcess</span>(<span class="title class_ inherited__">Process</span>):</span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params">self, name, mq</span>):</span><br><span class="line">        Process.__init__(self)</span><br><span class="line">        self.name = name</span><br><span class="line">        self.mq = mq</span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">run</span>(<span class="params">self</span>):</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">f&quot;process:<span class="subst">&#123;self.name&#125;</span>,start&quot;</span>)</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">f&quot;get data:<span class="subst">&#123;self.mq.get()&#125;</span>&quot;</span>)</span><br><span class="line">        sleep(<span class="number">3</span>)</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">f&quot;process:<span class="subst">&#123;self.name&#125;</span>,end&quot;</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&#x27;__main__&#x27;</span>:</span><br><span class="line">    mq = Queue()</span><br><span class="line">    mq.put(<span class="string">&quot;1&quot;</span>)</span><br><span class="line">    mq.put(<span class="string">&quot;2&quot;</span>)</span><br><span class="line">    mq.put(<span class="string">&quot;3&quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 进程列表</span></span><br><span class="line">    p_list = []</span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">3</span>):</span><br><span class="line">        p = MyProcess(<span class="string">f&quot;p<span class="subst">&#123;i&#125;</span>&quot;</span>, mq)</span><br><span class="line">        p_list.append(p)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> p <span class="keyword">in</span> p_list:</span><br><span class="line">        p.start()</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h4 id="Pipe管道实现进程通信"><a href="#Pipe管道实现进程通信" class="headerlink" title="Pipe管道实现进程通信"></a>Pipe管道实现进程通信</h4><p><img src="https://gitee.com/huaiyuechusan/picture/raw/master/Typora/202312302345225.png" alt="image-20231230213855878"></p>
<p>Pipe 直译过来的意思是“管”或“管道”，和实际生活中的管（管道）是非常类似的。</p>
<p>Pipe方法返回<code>（conn1， conn2）</code>代表一个管道的两个端。</p>
<blockquote>
<p>Pipe方法有duplex参数，如果duplex参数为True（默认值），那么这个参数是全双工模式，也就是说<code>conn1</code>和<code>conn2</code>均可收发。若duplex为False，<code>conn1</code>只负责接收消息，<code>conn2</code>只负责发送消息。send和recv方法分别是发送和接受消息的方法。例如，在全双工模式下，可以调用<code>conn1.send</code>发送消息，<code>conn1.recv</code>接收消息。如果没有消息可接收，recv方法会一直阻塞。如果管道已经被关闭，那么recv方法会抛出EOFError。</p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># coding=utf-8</span></span><br><span class="line"><span class="keyword">import</span> multiprocessing</span><br><span class="line"><span class="keyword">from</span> time <span class="keyword">import</span> sleep</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">func1</span>(<span class="params">conn1</span>):</span><br><span class="line">    sub_info = <span class="string">&quot;Hello!&quot;</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">f&quot;进程1--<span class="subst">&#123;multiprocessing.current_process().pid&#125;</span>发送数据：<span class="subst">&#123;sub_info&#125;</span>&quot;</span>)</span><br><span class="line">    sleep(<span class="number">1</span>)</span><br><span class="line">    conn1.send(sub_info)</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">f&quot;来自进程2:<span class="subst">&#123;conn1.recv()&#125;</span>&quot;</span>)</span><br><span class="line">    sleep(<span class="number">1</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">func2</span>(<span class="params">conn2</span>):</span><br><span class="line">    sub_info = <span class="string">&quot;你好!&quot;</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">f&quot;进程2--<span class="subst">&#123;multiprocessing.current_process().pid&#125;</span>发送数据：<span class="subst">&#123;sub_info&#125;</span>&quot;</span>)</span><br><span class="line">    sleep(<span class="number">1</span>)</span><br><span class="line">    conn2.send(sub_info)</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">f&quot;来自进程1:<span class="subst">&#123;conn2.recv()&#125;</span>&quot;</span>)</span><br><span class="line">    sleep(<span class="number">1</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&#x27;__main__&#x27;</span>:</span><br><span class="line">    <span class="comment"># 创建管道</span></span><br><span class="line">    conn1, conn2 = multiprocessing.Pipe()</span><br><span class="line">    <span class="comment"># 创建子进程</span></span><br><span class="line">    process1 = multiprocessing.Process(target=func1, args=(conn1,))</span><br><span class="line">    process2 = multiprocessing.Process(target=func2, args=(conn2,))</span><br><span class="line">    <span class="comment"># 启动子进程</span></span><br><span class="line">    process1.start()</span><br><span class="line">    process2.start()</span><br><span class="line"></span><br><span class="line">    </span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">进程1--22424发送数据：Hello!</span></span><br><span class="line"><span class="string">进程2--17168发送数据：你好!</span></span><br><span class="line"><span class="string">来自进程1:Hello!来自进程2:你好!</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br></pre></td></tr></table></figure>
<h4 id="Manager管理器实现进程通信"><a href="#Manager管理器实现进程通信" class="headerlink" title="Manager管理器实现进程通信"></a>Manager管理器实现进程通信</h4><p>管理器提供了一种创建共享数据的方法，从而可以在不同进程中共享。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># coding=utf-8</span></span><br><span class="line"><span class="keyword">from</span> multiprocessing <span class="keyword">import</span> Process, current_process</span><br><span class="line"><span class="keyword">from</span> multiprocessing <span class="keyword">import</span> Manager</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">func</span>(<span class="params">name, m_list, m_dict</span>):</span><br><span class="line">    m_dict[<span class="string">&#x27;name&#x27;</span>] = <span class="string">&#x27;尚学堂&#x27;</span></span><br><span class="line">    m_list.append(<span class="string">&#x27;你好&#x27;</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&#x27;__main__&#x27;</span>:</span><br><span class="line">    <span class="keyword">with</span> Manager() <span class="keyword">as</span> mgr:</span><br><span class="line">        m_list = mgr.<span class="built_in">list</span>()</span><br><span class="line">        m_dict = mgr.<span class="built_in">dict</span>()</span><br><span class="line">        m_list.append(<span class="string">&#x27;Hello!!&#x27;</span>)</span><br><span class="line">        <span class="comment"># 两个进程不能直接互相使用对象，需要互相传递</span></span><br><span class="line">        p1 = Process(target=func, args=(<span class="string">&#x27;p1&#x27;</span>, m_list, m_dict))</span><br><span class="line">        p1.start()</span><br><span class="line">        p1.join()  <span class="comment"># 等p1进程结束，主进程继续执行</span></span><br><span class="line">        <span class="built_in">print</span>(<span class="string">f&quot;主进程：<span class="subst">&#123;m_list&#125;</span>&quot;</span>)</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">f&quot;主进程：<span class="subst">&#123;m_dict&#125;</span>&quot;</span>)</span><br></pre></td></tr></table></figure>
<h4 id="进程池-Pool-管理进程的两种典型案例"><a href="#进程池-Pool-管理进程的两种典型案例" class="headerlink" title="进程池(Pool)管理进程的两种典型案例"></a>进程池(Pool)管理进程的两种典型案例</h4><p>Python提供了更好的管理多个进程的方式，就是使用进程池。</p>
<p><img src="https://gitee.com/huaiyuechusan/picture/raw/master/Typora/202312302345226.png" alt="image-20231230215358880"></p>
<p>进程池可以提供指定数量的进程给用户使用，即当有新的请求提交到进程池中时，如果池未满，则会创建一个新的进程用来执行该请求；反之，如果池中的进程数已经达到规定最大值，那么该请求就会等待，只要池中有进程空闲下来，该请求就能得到执行。</p>
<p>使用进程池的优点：</p>
<ol>
<li>提高效率，节省开辟进程和开辟内存空间的时间及销毁进程的时间</li>
<li>节省内存空间</li>
</ol>
<div class="table-container">
<table>
<thead>
<tr>
<th><strong>类/方法</strong></th>
<th><strong>功能</strong></th>
<th><strong>参数</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td><code>Pool(processes)</code></td>
<td>创建进程池对象</td>
<td>processes表示进程池中有多少进程</td>
</tr>
<tr>
<td><code>pool.apply_async(func,args,kwds)</code></td>
<td>异步执行；将事件放入到进程池队列</td>
<td>func 事件函数 args 以元组形式给func传参kwds 以字典形式给func传参 返回值：返回一个代表进程池事件的对象，通过返回值的get方法可以得到事件函数的返回值</td>
</tr>
<tr>
<td><code>pool.apply(func,args,kwds)</code></td>
<td>同步执行；将事件放入到进程池队列</td>
<td>func 事件函数 args 以元组形式给func传参kwds 以字典形式给func传参</td>
</tr>
<tr>
<td><code>pool.close()</code></td>
<td>关闭进程池</td>
<td></td>
</tr>
<tr>
<td><code>pool.join()</code></td>
<td>回收进程池</td>
<td></td>
</tr>
<tr>
<td><code>pool.map(func,iter)</code></td>
<td>类似于python的map函数，将要做的事件放入进程</td>
<td>func 要执行的函数 iter 迭代对象</td>
</tr>
</tbody>
</table>
</div>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># coding=utf-8</span></span><br><span class="line"><span class="keyword">import</span> os</span><br><span class="line"><span class="keyword">from</span> multiprocessing <span class="keyword">import</span> Pool</span><br><span class="line"><span class="keyword">from</span> time <span class="keyword">import</span> sleep</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">func1</span>(<span class="params">name</span>):</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">f&quot;打印当前进程的id:<span class="subst">&#123;os.getpid()&#125;</span>,<span class="subst">&#123;name&#125;</span>&quot;</span>)</span><br><span class="line">    sleep(<span class="number">2</span>)</span><br><span class="line">    <span class="keyword">return</span> name</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">func2</span>(<span class="params">args</span>):</span><br><span class="line">    <span class="built_in">print</span>(args)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&#x27;__main__&#x27;</span>:</span><br><span class="line">    pool = Pool(<span class="number">5</span>)</span><br><span class="line"></span><br><span class="line">    pool.apply_async(func=func1, args=(<span class="string">&quot;sxt1&quot;</span>,), callback=func2)</span><br><span class="line">    pool.apply_async(func=func1, args=(<span class="string">&quot;sxt2&quot;</span>,), callback=func2)</span><br><span class="line">    pool.apply_async(func=func1, args=(<span class="string">&quot;sxt3&quot;</span>,), callback=func2)</span><br><span class="line">    pool.apply_async(func=func1, args=(<span class="string">&quot;sxt4&quot;</span>,))</span><br><span class="line">    pool.apply_async(func=func1, args=(<span class="string">&quot;sxt5&quot;</span>,))</span><br><span class="line">    pool.apply_async(func=func1, args=(<span class="string">&quot;sxt6&quot;</span>,))</span><br><span class="line">    pool.apply_async(func=func1, args=(<span class="string">&quot;sxt7&quot;</span>,))</span><br><span class="line">    pool.apply_async(func=func1, args=(<span class="string">&quot;sxt8&quot;</span>,))</span><br><span class="line"></span><br><span class="line">    pool.close()</span><br><span class="line">    pool.join()</span><br><span class="line">    </span><br><span class="line">    </span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">打印当前进程的id:26676,sxt1</span></span><br><span class="line"><span class="string">打印当前进程的id:15944,sxt2</span></span><br><span class="line"><span class="string">打印当前进程的id:1352,sxt3</span></span><br><span class="line"><span class="string">打印当前进程的id:27176,sxt4</span></span><br><span class="line"><span class="string">打印当前进程的id:22592,sxt5</span></span><br><span class="line"><span class="string">打印当前进程的id:26676,sxt6</span></span><br><span class="line"><span class="string">sxt1</span></span><br><span class="line"><span class="string">打印当前进程的id:22592,sxt7</span></span><br><span class="line"><span class="string">打印当前进程的id:27176,sxt8</span></span><br><span class="line"><span class="string">sxt2</span></span><br><span class="line"><span class="string">sxt3</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br></pre></td></tr></table></figure>
<p>使用with管理进程池</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># coding=utf-8</span></span><br><span class="line"><span class="keyword">import</span> os</span><br><span class="line"><span class="keyword">from</span> multiprocessing <span class="keyword">import</span> Pool</span><br><span class="line"><span class="keyword">from</span> time <span class="keyword">import</span> sleep</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">func1</span>(<span class="params">name</span>):</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">f&quot;打印当前进程的id:<span class="subst">&#123;os.getpid()&#125;</span>,<span class="subst">&#123;name&#125;</span>&quot;</span>)</span><br><span class="line">    sleep(<span class="number">2</span>)</span><br><span class="line">    <span class="keyword">return</span> name</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&#x27;__main__&#x27;</span>:</span><br><span class="line">    <span class="keyword">with</span> Pool(<span class="number">5</span>) <span class="keyword">as</span> pool:</span><br><span class="line">        args = pool.<span class="built_in">map</span>(func1, (<span class="string">&quot;sxt1,&quot;</span>, <span class="string">&quot;sxt2,&quot;</span>, <span class="string">&quot;sxt3,&quot;</span>, <span class="string">&quot;sxt4,&quot;</span>, <span class="string">&quot;sxt5,&quot;</span>, <span class="string">&quot;sxt6,&quot;</span>, <span class="string">&quot;sxt7,&quot;</span>, <span class="string">&quot;sxt8,&quot;</span>))</span><br><span class="line">        <span class="keyword">for</span> a <span class="keyword">in</span> args:</span><br><span class="line">            <span class="built_in">print</span>(a)</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h3 id="协程"><a href="#协程" class="headerlink" title="协程"></a>协程</h3><p><img src="https://gitee.com/huaiyuechusan/picture/raw/master/Typora/202312302345227.png" alt="image-20231230221221571"></p>
<h4 id="核心概念面试重点"><a href="#核心概念面试重点" class="headerlink" title="核心概念面试重点"></a>核心概念面试重点</h4><p>协程，Coroutines，也叫作纤程(Fiber)协程，全称是“协同程序”，用来实现任务协作。是一种在线程中，比线程更加轻量级的存在，由<strong>程序员自己写程序来管理</strong>。</p>
<p>当出现IO阻塞时，CPU一直等待IO返回，处于空转状态。这时候用协程，可以执行其他任务。当IO返回结果后，再回来处理数据。充分利用了IO等待的时间，提高了效率。</p>
<p><img src="https://gitee.com/huaiyuechusan/picture/raw/master/Typora/202312302345228.png" alt="image-20231230231457305"></p>
<p><strong>协程的核心(控制流的让出和恢复)</strong></p>
<ol>
<li>每个协程有自己的执行栈，可以保存自己的执行现场</li>
<li>可以由用户程序按需创建协程（比如：遇到io操作）</li>
<li>协程“主动让出（yield）”执行权时候,会保存执行现场(保存中断时的寄存器上下文和栈)，然后切换到其他协程</li>
<li>协程恢复执行（resume）时，根据之前保存的执行现场恢复到中断前的状态，继续执行，这样就通过协程实现了轻量的由用户态调度的多任务模型</li>
</ol>
<p><strong>协程和多线程比较</strong></p>
<p>比如，有3个任务需要完成，每个任务都在等待I/O操作时阻塞自身。阻塞在I/O操作上所花费的时间已经用灰色框标示出来了。</p>
<p><img src="https://gitee.com/huaiyuechusan/picture/raw/master/Typora/202312302345229.png" alt="image-20231230231731772"></p>
<ol>
<li>在单线程同步模型中，任务按照顺序执行。如果某个任务因为I/O而阻塞，其他所有的任务都必须等待，直到它完成之后它们才能依次执行。</li>
<li>多线程版本中，这3个任务分别在独立的线程中执行。这些线程由操作系统来管理，在多处理器系统上可以并行处理，或者在单处理器系统上交错执行。这使得当某个线程阻塞在某个资源的同时其他线程得以继续执行。</li>
<li>协程版本的程序中，3个任务交错执行，但仍然在一个单独的线程控制中。当处理I/O或者其他昂贵的操作时，注册一个回调到事件循环中，然后当I/O操作完成时继续执行。回调描述了该如何处理某个事件。事件循环轮询所有的事件，当事件到来时将它们分配给等待处理事件的回调函数。</li>
</ol>
<p><strong>协程的优点</strong></p>
<ol>
<li>由于自身带有上下文和栈，无需线程上下文切换的开销,属于程序级别的切换，操作系统完全感知不到，因而更加==轻量级==；</li>
<li>无需原子操作的锁定及同步的开销</li>
<li>方便切换控制流，简化编程模型</li>
<li>单线程内就可以实现并发的效果，最大限度地利用cpu，且可扩展性高，成本低（注：一个CPU支持上万的协程都不是问题。所以很适合用于高并发处理）</li>
</ol>
<blockquote>
<p>asyncio协程是写爬虫比较好的方式。比多线程和多进程都好.</p>
<p>开辟新的线程和进程是非常耗时的。</p>
</blockquote>
<p><strong>协程的缺点</strong></p>
<ol>
<li>无法利用多核资源：协程的本质是个单线程,它不能同时将 单个CPU 的多个核用上,协程需要和进程配合才能运行在多CPU上。</li>
<li>当然我们日常所编写的绝大部分应用都没有这个必要，除非是cpu密集型应用。</li>
</ol>
<h4 id="yield方式实现（已淘汰，了解即可）"><a href="#yield方式实现（已淘汰，了解即可）" class="headerlink" title="yield方式实现（已淘汰，了解即可）"></a>yield方式实现（已淘汰，了解即可）</h4><p>Python中的协程经历了很长的一段发展历程。其大概经历了如下三个阶段：</p>
<ol>
<li>最初的生成器变形 <code>yield/send</code></li>
<li>引入 <code>@asyncio.coroutine</code> 和 <code>yield from</code></li>
<li>Python3.5版本后，引入 <code>async/await</code> 关键字</li>
</ol>
<h4 id="asyncio异步IO实现协程-重点）"><a href="#asyncio异步IO实现协程-重点）" class="headerlink" title="asyncio异步IO实现协程(重点）"></a>asyncio异步IO实现协程(重点）</h4><ol>
<li>正常的函数执行时是不会中断的，所以你要写一个能够中断的函数，就需要加 <code>async</code></li>
<li><code>async</code> 用来声明一个函数为异步函数，异步函数的特点是能在函数执行过程中挂起，去执行其他异步函数，等到挂起条件（假设挂起条件是 <code>sleep(5)</code> ）消失后，也就是5秒到了再回来执行</li>
<li><code>await</code> 用来用来声明程序挂起，比如异步程序执行到某一步时需要等待的时间很长，就将此挂起，去执行其他的异步程序。</li>
<li><code>asyncio</code> 是python3.5之后的协程模块，是python实现并发重要的包，这个包使用<strong>事件循环</strong>驱动实现并发。</li>
</ol>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># coding=utf-8</span></span><br><span class="line"><span class="keyword">import</span> asyncio</span><br><span class="line"><span class="keyword">import</span> time</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">async</span> <span class="keyword">def</span> <span class="title function_">func1</span>():  <span class="comment"># async表示方法是异步的</span></span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">3</span>):</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">f&#x27;北京：第<span class="subst">&#123;i&#125;</span>次打印啦&#x27;</span>)</span><br><span class="line">        <span class="keyword">await</span> asyncio.sleep(<span class="number">1</span>)</span><br><span class="line">    <span class="keyword">return</span> <span class="string">&quot;func1执行完毕&quot;</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">async</span> <span class="keyword">def</span> <span class="title function_">func2</span>():</span><br><span class="line">    <span class="keyword">for</span> k <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">3</span>):</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">f&#x27;上海：第<span class="subst">&#123;k&#125;</span>次打印了&#x27;</span>)</span><br><span class="line">        <span class="keyword">await</span> asyncio.sleep(<span class="number">1</span>)</span><br><span class="line">    <span class="keyword">return</span> <span class="string">&quot;func2执行完毕&quot;</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">async</span> <span class="keyword">def</span> <span class="title function_">main</span>():</span><br><span class="line">    res = <span class="keyword">await</span> asyncio.gather(func1(), func2())</span><br><span class="line">    <span class="comment"># await异步执行func1方法</span></span><br><span class="line">    <span class="comment"># 返回值为函数的返回值列表,本例为[&quot;func1执行完毕&quot;, &quot;func2执行完毕&quot;]</span></span><br><span class="line">    <span class="built_in">print</span>(res)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&#x27;__main__&#x27;</span>:</span><br><span class="line">    start_time = time.time()</span><br><span class="line">    asyncio.run(main())</span><br><span class="line">    end_time = time.time()</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">f&quot;耗时<span class="subst">&#123;end_time - start_time&#125;</span>&quot;</span>)  <span class="comment"># 耗时3秒,效率极大提高</span></span><br><span class="line"></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">北京：第0次打印啦</span></span><br><span class="line"><span class="string">上海：第0次打印了</span></span><br><span class="line"><span class="string">北京：第1次打印啦</span></span><br><span class="line"><span class="string">上海：第1次打印了</span></span><br><span class="line"><span class="string">北京：第2次打印啦</span></span><br><span class="line"><span class="string">上海：第2次打印了</span></span><br><span class="line"><span class="string">[&#x27;func1执行完毕&#x27;, &#x27;func2执行完毕&#x27;]</span></span><br><span class="line"><span class="string">耗时3.0080811977386475</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br></pre></td></tr></table></figure>
<h2 id="网络通信"><a href="#网络通信" class="headerlink" title="网络通信"></a>网络通信</h2><h3 id="基本概念"><a href="#基本概念" class="headerlink" title="基本概念"></a>基本概念</h3><h4 id="IP地址"><a href="#IP地址" class="headerlink" title="IP地址"></a>IP地址</h4><p><img src="https://gitee.com/huaiyuechusan/picture/raw/master/Typora/202401061923216.png" alt="image-20231230234542948"></p>
<p>IP是Internet Protocol Address，即”互联网协议地址”。</p>
<p>用来标识网络中的一个通信实体的地址。通信实体可以是计算机、路由器等。 比如互联网的每个服务器都要有自己的IP地址，而每个局域网的计算机要通信也要配置IP地址。</p>
<p>路由器是连接两个或多个网络的网络设备。</p>
<p><img src="https://gitee.com/huaiyuechusan/picture/raw/master/Typora/202401061923217.png" alt="image-20231230234844602"></p>
<p>IP地址实际上是一个32位整数（称为IPv4），以字符串表示的IP地址如 <code>192.168.0.1</code> 实际上是把32位整数按8位分组后的数字表示，目的是便于阅读。</p>
<p>IPv6地址实际上是一个128位整数，它是目前使用的IPv4的升级版，以字符串表示类似于 <code>2001:0db8:85a3:0042:1000:8a2e:0370:7334</code></p>
<p><strong>公有地址</strong></p>
<p>公有地址（Public address）由Inter NIC（Internet NetworkInformation Center互联网信息中心）负责。这些IP地址分配给注册并向Inter NIC提出申请的组织机构。通过它直接访问互联网。</p>
<p><strong>私有地址</strong></p>
<p>私有地址（Private address）属于非注册地址，专门为组织机构内部使用。</p>
<p>以下列出留用的内部私有地址</p>
<p>A类 10.0.0.0—10.255.255.255</p>
<p>B类 172.16.0.0—172.31.255.255</p>
<p>C类 192.168.0.0—192.168.255.255</p>
<h4 id="端口port"><a href="#端口port" class="headerlink" title="端口port"></a>端口port</h4><p>端口号用来识别计算机中进行通信的应用程序。因此，它也被称为程序地址。</p>
<p>一台计算机上同时可以运行多个程序。传输层协议正是利用这些端口号识别本机中正在进行通信的应用程序，并准确地进行数据传输。</p>
<p><img src="https://gitee.com/huaiyuechusan/picture/raw/master/Typora/202401061923218.png" alt="image-20231230235342700"></p>
<p><strong>端口分配</strong></p>
<p>端口是虚拟的概念，并不是说在主机上真的有若干个端口。通过端口，可以在一个主机上运行多个网络应用程序。 端口的表示是一个16位的二进制整数，对应十进制的0-65535。操作系统中一共提供了0~65535可用端口范围。</p>
<p>按端口号分类：</p>
<p><strong>公认端口（Well Known Ports）：</strong>从0到1023，它们紧密绑定（binding）于一些服务。通常这些端口的通讯明确表明了某种服务</p>
<p>的协议。例如：80端口实际上总是HTTP通讯</p>
<p><strong>注册端口（Registered Ports）：</strong>从1024到65535。它们松散地绑定于一些服务。也就是说有许多服务绑定于这些端口，这些        端        口同样用于许多其它目的。例如：许多系统处理动态端口从1024左右开始。</p>
<h4 id="网络通信协议"><a href="#网络通信协议" class="headerlink" title="网络通信协议"></a>网络通信协议</h4><p>国际标准化组织(ISO，即International Organization forStandardization)定义了网络通信协议的基本框架，被称为OSI（Open System Interconnect，即开放系统互联）模型。</p>
<p>OSI模型制定的七层标准模型，分别是：应用层，表示层，会话层，传输层，网络层，数据链路层，物理层。</p>
<p>OSI七层协议模型如图所示：</p>
<p><img src="https://gitee.com/huaiyuechusan/picture/raw/master/Typora/202401061923219.png" alt="image-20231230235839168"></p>
<h4 id="网络通信协议的分层-ISO分层模型"><a href="#网络通信协议的分层-ISO分层模型" class="headerlink" title="网络通信协议的分层-ISO分层模型"></a>网络通信协议的分层-ISO分层模型</h4><p>虽然国际标准化组织制定了这样一个网络通信协议的模型，但是实际上互联网通讯使用最多的网络通信协议是TCP/IP网络通信协议。</p>
<p>TCP/IP 是一个协议族，也是按照层次划分，共四层：应用层，传输层，互连网络层，网络接口层（物理+数据链路层）。</p>
<p>把用户应用程序作为最高层，把物理通信线路作为最低层，将其间的协议处理分为若干层，规定每层处理的任务，也规定每层的接口标准。</p>
<p>ISO模型与TCP/IP模型的对应关系如图所示。</p>
<p><img src="https://gitee.com/huaiyuechusan/picture/raw/master/Typora/202401061923220.png" alt="image-20231230235947847"></p>
<h4 id="TCP和UDP传输数据的区别"><a href="#TCP和UDP传输数据的区别" class="headerlink" title="TCP和UDP传输数据的区别"></a>TCP和UDP传输数据的区别</h4><p>TCP 和 UDP 的优缺点无法简单地、绝对地去做比较：</p>
<p>TCP 用于在传输层有必要实现<strong>可靠</strong>传输的情况；</p>
<p>UDP 主要用于那些对高速传输和实时性有较高要求的通信或广播通信。</p>
<p>TCP 和 UDP 应该根据应用的目的按需使用。</p>
<p><strong>TCP</strong></p>
<p>TCP（Transmission Control Protocol，传输控制协议）。TCP方式就类似于拨打电话，使用该种方式进行网络通讯时，需要建立专门的虚拟连接，然后进行可靠的数据传输，如果数据发送失败，则客户端会自动重发该数据。</p>
<p><strong>UDP</strong></p>
<p>UDP（User Data Protocol，用户数据报协议）</p>
<p>UDP是一个非连接的协议，传输数据之前源端和终端不建立连接，当它想传送时就简单地去抓取来自应用程序的数据，并尽可能快地把它扔到网络上。 在发送端，UDP传送数据的速度仅仅是受应用程序生成数据的速度、 计算机的能力和传输带宽的限制； 在接收端，UDP把每个消息段放在队列中，应用程序每次从队列中读一个消息段。</p>
<p>UDP方式就类似于发送短信，使用这种方式进行网络通讯时，不需要建立专门的虚拟连接，传输也不是很可靠，如果发送失败则客户端无法获得。</p>
<p>UDP 因为没有拥塞控制，一直会以恒定的速度发送数据。即使网络条件不好，也不会对发送速率进行调整。这样实现的弊端就是在网络条件不好的情况下可能会导致丢包，但是优点也很明显，在某些实时性要求高的场景（比如电话会议）就需要使用 UDP 而不是 TCP。</p>
<p><strong>TCP和UDP区别</strong></p>
<p>这两种传输方式都在实际的网络编程中使用，重要的数据一般使用TCP方式进行数据传输，而大量的非核心数据则可以通过UDP方式进行传递，在一些程序中甚至结合使用这两种方式进行数据传递。由于TCP需要建立专用的虚拟连接以及确认传输是否正确，所以使用TCP方式的速度稍微慢一些，而且传输时产生的数据量要比UDP稍微大一些。</p>
<div class="table-container">
<table>
<thead>
<tr>
<th></th>
<th><strong>UDP</strong></th>
<th><strong>TCP</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td>是否连接</td>
<td>无连接</td>
<td>面向连接</td>
</tr>
<tr>
<td>是否可靠</td>
<td>不可靠传输，不使用流量控制和拥塞控制</td>
<td>可靠传输，使用流量控制和拥塞控制</td>
</tr>
<tr>
<td>连接对象个数</td>
<td>支持一对一，一对多，多对一和多对多交互通信</td>
<td>只能是一对一通信</td>
</tr>
<tr>
<td>传输方式</td>
<td>面向报文</td>
<td>面向字节流</td>
</tr>
<tr>
<td>首部开销</td>
<td>首部开销小，仅8字节</td>
<td>首部最小20字节，最大60字节</td>
</tr>
<tr>
<td>适用场景</td>
<td>适用于实时应用（IP电话、视频会议、直播等）</td>
<td>适用于要求可靠传输的应用，例如文件传输</td>
</tr>
</tbody>
</table>
</div>
<blockquote>
<p><strong>总结</strong></p>
<p>TCP是面向连接的，传输数据安全，稳定，效率相对较低。</p>
<p>UDP是面向无连接的，传输数据不安全，效率较高。</p>
</blockquote>
<h4 id="TCP建立连接的三次握手"><a href="#TCP建立连接的三次握手" class="headerlink" title="TCP建立连接的三次握手"></a>TCP建立连接的三次握手</h4><p><img src="https://gitee.com/huaiyuechusan/picture/raw/master/Typora/202401061923222.png" alt="image-20231231164116510"></p>
<p>TCP是面向连接的协议，也就是说，在收发数据前，必须和对方建立可靠的连接。 一个TCP连接必须要经过三次“对话”才能建立起来，其中的过程非常复杂， 只简单的描述下这三次对话的简单过程：</p>
<p>1）主机A向主机B发出连接请求：“我想给你发数据，可以吗？”，这是第一次对话；</p>
<p>2）主机B向主机A发送同意连接和要求同步 （同步就是两台主机一个在发送，一个在接收，协调工作）的数据包 ：“可以，你什么时              候发？”，这是第二次对话；</p>
<p>3）主机A再发出一个数据包确认主机B的要求同步：“我现在就发，你接着吧！”， 这是第三次握手。三次“对话”的目的是使数据包              的发送和接收同步， 经过三次“对话”之后，主机A才向主机B正式发送数据。</p>
<p><img src="https://gitee.com/huaiyuechusan/picture/raw/master/Typora/202401061923223.png" alt="image-20231231164057483"></p>
<p><img src="https://gitee.com/huaiyuechusan/picture/raw/master/Typora/202401061923224.png" alt="image-20231231164208963"></p>
<ol>
<li>第一步，客户端发送一个包含SYN即同步（Synchronize）标志的TCP报文，SYN同步报文会指明客户端使用的端口以及TCP连接的初始序号。</li>
<li>第二步，服务器在收到客户端的SYN报文后，将返回一个SYN+ACK的报文，表示客户端的请求被接受，同时TCP序号被加一，ACK即确认（Acknowledgement）</li>
<li>第三步，客户端也返回一个确认报文ACK给服务器端，同样TCP序列号被加一，到此一个TCP连接完成。然后才开始通信的第二步：数据处理。</li>
</ol>
<p>这就是所说的TCP的三次握手（Three-way Handshake）。</p>
<blockquote>
<p>为什么TCP协议有三次握手,而UDP协议没有？</p>
<p>因为三次握手的目的是在client端和server端建立可靠的连接。保证双方发送的数据对方都能接受到，这也是TCP协议的被</p>
<p>称为可靠的数据传输协议的原因。而UDP就不一样，UDP不提供可靠的传输模式，发送端并不需要得到接收端的状态，因此</p>
<p>UDP协议就用不着使用三次握手。</p>
</blockquote>
<h4 id="TCP断开连接的四次挥手"><a href="#TCP断开连接的四次挥手" class="headerlink" title="TCP断开连接的四次挥手"></a>TCP断开连接的四次挥手</h4><p><img src="https://gitee.com/huaiyuechusan/picture/raw/master/Typora/202401061923225.png" alt="image-20231231164341302"></p>
<p><strong>TCP建立连接要进行3次握手，而断开连接要进行4次：</strong></p>
<p>第一次： 当主机A完成数据传输后,将控制位FIN置1，提出停止TCP连接的请求 ；</p>
<p>第二次： 主机B收到FIN后对其作出响应，确认这一方向上的TCP连接将关闭,将ACK置1；</p>
<p>第三次： 由B 端再提出反方向的关闭请求,将FIN置1；</p>
<p>第四次： 主机A对主机B的请求进行确认，将ACK置1，双方向的关闭结束。</p>
<p>由TCP的三次握手和四次断开可以看出，TCP使用面向连接的通信方式， 大大提高了数据通信的可靠性，使发送数据端和接收端在数据</p>
<p>正式传输前就有了交互， 为数据正式传输打下了可靠的基础。</p>
<h4 id="数据包与处理流程"><a href="#数据包与处理流程" class="headerlink" title="数据包与处理流程"></a>数据包与处理流程</h4><p><strong>什么是数据包</strong></p>
<p>通信传输中的数据单位，一般也称“数据包”。在数据包中包括：</p>
<p>包、帧、数据包、段、消息。</p>
<p>网络中传输的数据包由两部分组成：一部分是协议所要用到的首部，另一部分是上一层传过来的数据。首部的结构由协议的具体规</p>
<p>范详细定义。在数据包的首部，明确标明了协议应该如何读取数据。反过来说，看到首部，也就能够了解该协议必要的信息以及所</p>
<p>要处理的数据。包首部就像协议的脸。</p>
<p><img src="https://gitee.com/huaiyuechusan/picture/raw/master/Typora/202401061923226.png" alt="image-20231231164541622"></p>
<p><strong>数据包处理流程</strong></p>
<p><img src="https://gitee.com/huaiyuechusan/picture/raw/master/Typora/202401061923227.png" alt="image-20231231204815467"></p>
<h3 id="套接字编程实战"><a href="#套接字编程实战" class="headerlink" title="套接字编程实战"></a>套接字编程实战</h3><h4 id="socket编程介绍"><a href="#socket编程介绍" class="headerlink" title="socket编程介绍"></a>socket编程介绍</h4><p>TCP协议和UDP协议是传输层的两种协议。Socket是==传输层供给应用层的编程接口==，所以Socket编程就分为TCP编程和UDP编程两类。</p>
<p><img src="https://gitee.com/huaiyuechusan/picture/raw/master/Typora/202401061923228.png" alt="image-20231231205201674"></p>
<p>Socket编程封装了常见的TCP、UDP操作，可以实现非常方便的网络编程。</p>
<p><img src="https://gitee.com/huaiyuechusan/picture/raw/master/Typora/202401061923229.png" alt="image-20231231205316288"></p>
<h4 id="socket-函数介绍"><a href="#socket-函数介绍" class="headerlink" title="socket()函数介绍"></a>socket()函数介绍</h4><p>在Python语言标准库中，通过使用socket模块提供的socket对象，可以在计算机网络中建立可以互相通信的服务器与客户端。在服务</p>
<p>器端需要建立一个socket对象，并等待客户端的连接。客户端使用socket对象与服务器端进行连接，一旦连接成功，客户端和服务器</p>
<p>端就可以进行通信了。</p>
<p><img src="https://gitee.com/huaiyuechusan/picture/raw/master/Typora/202401061923230.png" alt="image-20231231205344694"></p>
<blockquote>
<p>:warning:上图中，我们可以看出socket通讯中，发送和接收数据，都是通过操作系统控制网卡来进行。因此，我们在使用之后，必须关闭socket。</p>
</blockquote>
<p>在Python 中，通常用一个Socket表示“打开了一个网络连接”，语法格式如下：</p>
<p><code>socket.socket([family[, type[, proto]]])</code></p>
<p><code>family</code> : 套接字家族可以使 <code>AF_UNIX</code> 或者 <code>AF_INET</code> ；</p>
<blockquote>
<p>AF 表示ADDRESS FAMILY 地址族</p>
<p>AF_INET（又称 PF_INET）是 IPv4 网络协议的套接字类型；而AF_UNIX 则是 Unix 系统本地通信。</p>
</blockquote>
<p><code>type</code> : 套接字类型可以根据是面向连接的还是非连接分为 <code>SOCK_STREAM</code>或 <code>SOCK_DGRAM</code> ； </p>
<p><code>protocol</code> : 一般不填，默认为0。</p>
<p>Socket主要分为面向连接的Socket和无连接的Socket。</p>
<p>无连接Socket的主要协议是用户数据报协议，也就是常说的UDP，UDP Socket的名字是 <code>SOCK_DGRAM</code> 。创建套接字UDP/IP套接字，可以调用 <code>socket.socket()</code> 。示例代码如下    ：</p>
<p><code>udpSocket=socket.socket (AF_INET,SOCK_DGRAM)</code></p>
<h4 id="socket对象的内置函数和属性"><a href="#socket对象的内置函数和属性" class="headerlink" title="socket对象的内置函数和属性"></a>socket对象的内置函数和属性</h4><p><img src="https://gitee.com/huaiyuechusan/picture/raw/master/Typora/202401061923231.png" alt="image-20240101001210654"></p>
<p><img src="https://gitee.com/huaiyuechusan/picture/raw/master/Typora/202401061923232.png" alt="image-20240101001227868"></p>
<p><img src="https://gitee.com/huaiyuechusan/picture/raw/master/Typora/202401061923233.png" alt="image-20240101001236545"></p>
<h4 id="UDP编程介绍"><a href="#UDP编程介绍" class="headerlink" title="UDP编程介绍"></a>UDP编程介绍</h4><p>UDP协议时，不需要建立连接，只需要知道对方的IP地址和端口号，就可以直接发数据包。但是，能不能到达就不知道了。虽然用UDP传输数据不可靠，但它的优点是和TCP比，速度快，对于不要求可靠到达的数据，就可以使用UDP协议。</p>
<p>创建Socket时， <code>SOCK_DGRAM</code> 指定了这个Socket的类型是UDP。绑定端口和TCP一样，但是不需要调用 <code>listen()</code> 方法，而是直接接收来自任何客户端的数据。 <code>recvfrom()</code> 方法返回数据和客户端的地址与端口，这样，服务器收到数据后，直接调用 <code>sendto()</code> 就可以把数据用UDP发给客户端。</p>
<h4 id="UDP编程的实现"><a href="#UDP编程的实现" class="headerlink" title="UDP编程的实现"></a>UDP编程的实现</h4><p>UDP接收数据</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># coding=utf-8</span></span><br><span class="line"><span class="keyword">from</span> socket <span class="keyword">import</span> *</span><br><span class="line"></span><br><span class="line"><span class="comment"># 最简化的UDP服务端代码</span></span><br><span class="line">s = socket(AF_INET, SOCK_DGRAM)  <span class="comment"># 创建UDP类型的套接字</span></span><br><span class="line">s.bind((<span class="string">&quot;127.0.0.1&quot;</span>, <span class="number">8888</span>))  <span class="comment"># 绑定端口，IP可以不写</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;等待接收的数据！&quot;</span>)</span><br><span class="line">recv_data = s.recvfrom(<span class="number">1024</span>)  <span class="comment"># 1024表示本次接受的最大字节数</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">f&quot;收到的远程信息：<span class="subst">&#123;recv_data[<span class="number">0</span>].decode(<span class="string">&#x27;gbk&#x27;</span>)&#125;</span>, form<span class="subst">&#123;recv_data[<span class="number">1</span>]&#125;</span>&quot;</span>)</span><br><span class="line">s.close()</span><br></pre></td></tr></table></figure>
<p>UDP发送数据</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># coding=utf-8</span></span><br><span class="line"><span class="keyword">from</span> socket <span class="keyword">import</span> *</span><br><span class="line"></span><br><span class="line"><span class="comment"># 最简化的UDP客户端发送消息代码</span></span><br><span class="line">s = socket(AF_INET, SOCK_DGRAM)  <span class="comment"># 创建UDP类型的套接字</span></span><br><span class="line">addr = (<span class="string">&quot;127.0.0.1&quot;</span>, <span class="number">8888</span>)</span><br><span class="line">data = <span class="built_in">input</span>(<span class="string">&quot;请输入：&quot;</span>)</span><br><span class="line">s.sendto(data.encode(<span class="string">&quot;gbk&quot;</span>), addr)</span><br><span class="line">s.close()</span><br></pre></td></tr></table></figure>
<h4 id="UDP持续通信"><a href="#UDP持续通信" class="headerlink" title="UDP持续通信"></a>UDP持续通信</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># coding=utf-8</span></span><br><span class="line"><span class="keyword">from</span> socket <span class="keyword">import</span> *</span><br><span class="line"></span><br><span class="line"><span class="comment"># UDP服务端持续接收消息代码</span></span><br><span class="line">s = socket(AF_INET, SOCK_DGRAM)  <span class="comment"># 创建UDP类型的套接字</span></span><br><span class="line">s.bind((<span class="string">&quot;127.0.0.1&quot;</span>, <span class="number">8888</span>))  <span class="comment"># 绑定端口，IP可以不写</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;等待接收的数据！&quot;</span>)</span><br><span class="line"><span class="keyword">while</span> <span class="literal">True</span>:</span><br><span class="line">    recv_data = s.recvfrom(<span class="number">1024</span>)  <span class="comment"># 1024表示本次接受的最大字节数</span></span><br><span class="line">    recv_content = recv_data[<span class="number">0</span>].decode(<span class="string">&#x27;gbk&#x27;</span>)</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">f&quot;收到的远程信息：<span class="subst">&#123;recv_content&#125;</span>, form<span class="subst">&#123;recv_data[<span class="number">1</span>]&#125;</span>&quot;</span>)</span><br><span class="line">    <span class="keyword">if</span> recv_content == <span class="string">&quot;88&quot;</span>:</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;结束聊天&quot;</span>)</span><br><span class="line">        <span class="keyword">break</span></span><br><span class="line">s.close()</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># coding=utf-8</span></span><br><span class="line"><span class="keyword">from</span> socket <span class="keyword">import</span> *</span><br><span class="line"></span><br><span class="line"><span class="comment"># UDP客户端持续发送消息代码</span></span><br><span class="line">s = socket(AF_INET, SOCK_DGRAM)  <span class="comment"># 创建UDP类型的套接字</span></span><br><span class="line">addr = (<span class="string">&quot;127.0.0.1&quot;</span>, <span class="number">8888</span>)</span><br><span class="line"><span class="keyword">while</span> <span class="literal">True</span>:</span><br><span class="line">    data = <span class="built_in">input</span>(<span class="string">&quot;请输入：&quot;</span>)</span><br><span class="line">    s.sendto(data.encode(<span class="string">&quot;gbk&quot;</span>), addr)</span><br><span class="line">    <span class="keyword">if</span> data == <span class="string">&quot;88&quot;</span>:</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;结束聊天&quot;</span>)</span><br><span class="line">        <span class="keyword">break</span></span><br><span class="line">s.close()</span><br></pre></td></tr></table></figure>
<h4 id="结合多线程实现UDP双向自由通信"><a href="#结合多线程实现UDP双向自由通信" class="headerlink" title="结合多线程实现UDP双向自由通信"></a>结合多线程实现UDP双向自由通信</h4><blockquote>
<p>UDP 不同于 TCP，不存在请求连接和受理过程，因此在某种意义上无法明确区分服务器端和客户端，只是因为其提供服务而</p>
<p>称为服务器端。</p>
</blockquote>
<p>如下服务端、客户端代码几乎一模一样，注意接收和发送端口对应，即可。</p>
<p><strong>UDP实现多线程服务端</strong></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># coding=utf-8</span></span><br><span class="line"><span class="keyword">from</span> socket <span class="keyword">import</span> *</span><br><span class="line"></span><br><span class="line"><span class="comment"># UDP实现多线程服务端代码</span></span><br><span class="line"><span class="keyword">from</span> threading <span class="keyword">import</span> Thread</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">recv_data</span>():</span><br><span class="line">    <span class="keyword">while</span> <span class="literal">True</span>:</span><br><span class="line">        recv_data = s.recvfrom(<span class="number">1024</span>)  <span class="comment"># 1024表示本次接受的最大字节数</span></span><br><span class="line">        recv_content = recv_data[<span class="number">0</span>].decode(<span class="string">&#x27;gbk&#x27;</span>)</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">f&quot;收到的远程信息：<span class="subst">&#123;recv_content&#125;</span>, form<span class="subst">&#123;recv_data[<span class="number">1</span>]&#125;</span>&quot;</span>)</span><br><span class="line">        <span class="keyword">if</span> recv_content == <span class="string">&quot;88&quot;</span>:</span><br><span class="line">            <span class="built_in">print</span>(<span class="string">&quot;结束聊天&quot;</span>)</span><br><span class="line">            <span class="keyword">break</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">send_data</span>():</span><br><span class="line">    addr = (<span class="string">&quot;127.0.0.1&quot;</span>, <span class="number">9999</span>)</span><br><span class="line">    <span class="keyword">while</span> <span class="literal">True</span>:</span><br><span class="line">        data = <span class="built_in">input</span>(<span class="string">&quot;请输入：&quot;</span>)</span><br><span class="line">        s.sendto(data.encode(<span class="string">&quot;gbk&quot;</span>), addr)</span><br><span class="line">        <span class="keyword">if</span> data == <span class="string">&quot;88&quot;</span>:</span><br><span class="line">            <span class="built_in">print</span>(<span class="string">&quot;结束聊天&quot;</span>)</span><br><span class="line">            <span class="keyword">break</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&#x27;__main__&#x27;</span>:</span><br><span class="line">    s = socket(AF_INET, SOCK_DGRAM)  <span class="comment"># 创建UDP类型的套接字</span></span><br><span class="line">    s.bind((<span class="string">&quot;127.0.0.1&quot;</span>, <span class="number">8888</span>))  <span class="comment"># 绑定端口，IP可以不写</span></span><br><span class="line">    <span class="comment"># 创建两个线程</span></span><br><span class="line">    t1 = Thread(target=recv_data)</span><br><span class="line">    t2 = Thread(target=send_data)</span><br><span class="line">    t1.start()</span><br><span class="line">    t2.start()</span><br><span class="line">    t1.join()</span><br><span class="line">    t2.join()</span><br></pre></td></tr></table></figure>
<p><strong>UDP实现多线程客户端</strong></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># coding=utf-8</span></span><br><span class="line"><span class="keyword">from</span> socket <span class="keyword">import</span> *</span><br><span class="line"></span><br><span class="line"><span class="comment"># UDP实现多线程客户端代码</span></span><br><span class="line"><span class="keyword">from</span> threading <span class="keyword">import</span> Thread</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">recv_data</span>():</span><br><span class="line">    <span class="keyword">while</span> <span class="literal">True</span>:</span><br><span class="line">        recv_data = s.recvfrom(<span class="number">1024</span>)  <span class="comment"># 1024表示本次接受的最大字节数</span></span><br><span class="line">        recv_content = recv_data[<span class="number">0</span>].decode(<span class="string">&#x27;gbk&#x27;</span>)</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">f&quot;收到的远程信息：<span class="subst">&#123;recv_content&#125;</span>, form<span class="subst">&#123;recv_data[<span class="number">1</span>]&#125;</span>&quot;</span>)</span><br><span class="line">        <span class="keyword">if</span> recv_content == <span class="string">&quot;88&quot;</span>:</span><br><span class="line">            <span class="built_in">print</span>(<span class="string">&quot;结束聊天&quot;</span>)</span><br><span class="line">            <span class="keyword">break</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">send_data</span>():</span><br><span class="line">    addr = (<span class="string">&quot;127.0.0.1&quot;</span>, <span class="number">8888</span>)</span><br><span class="line">    <span class="keyword">while</span> <span class="literal">True</span>:</span><br><span class="line">        data = <span class="built_in">input</span>(<span class="string">&quot;请输入：&quot;</span>)</span><br><span class="line">        s.sendto(data.encode(<span class="string">&quot;gbk&quot;</span>), addr)</span><br><span class="line">        <span class="keyword">if</span> data == <span class="string">&quot;88&quot;</span>:</span><br><span class="line">            <span class="built_in">print</span>(<span class="string">&quot;结束聊天&quot;</span>)</span><br><span class="line">            <span class="keyword">break</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&#x27;__main__&#x27;</span>:</span><br><span class="line">    s = socket(AF_INET, SOCK_DGRAM)  <span class="comment"># 创建UDP类型的套接字</span></span><br><span class="line">    s.bind((<span class="string">&quot;127.0.0.1&quot;</span>, <span class="number">9999</span>))  <span class="comment"># 绑定端口，IP可以不写</span></span><br><span class="line">    <span class="comment"># 创建两个线程</span></span><br><span class="line">    t1 = Thread(target=recv_data)</span><br><span class="line">    t2 = Thread(target=send_data)</span><br><span class="line">    t1.start()</span><br><span class="line">    t2.start()</span><br><span class="line">    t1.join()</span><br><span class="line">    t2.join()</span><br></pre></td></tr></table></figure>
<h4 id="TCP编程介绍"><a href="#TCP编程介绍" class="headerlink" title="TCP编程介绍"></a>TCP编程介绍</h4><p>面向连接的Socket使用的协议是TCP协议。TCP的Socket名称是<code>SOCK_STREAM</code> 。创建套接字TCP套接字，可以调用 <code>socket.socket()</code> 。示例代码如下：</p>
<p><code>tcpSocket=socket.socket(AF_INET,SOCK_STREAM)</code></p>
<p><img src="https://gitee.com/huaiyuechusan/picture/raw/master/Typora/202401061923234.png" alt="image-20240106181134943"></p>
<h4 id="TCP编程的实现"><a href="#TCP编程的实现" class="headerlink" title="TCP编程的实现"></a>TCP编程的实现</h4><p>在Python语言中创建Socket服务端程序，需要使用socket模块中的socket类。创建Socket服务器程序的步骤如下：</p>
<p>（1） 创建Socket对象。</p>
<p>（2） 绑定端口号。</p>
<p>（3） 监听端口号。</p>
<p>（4） 等待客户端Socket的连接。</p>
<p>（5） 读取客户端发送过来的数据。</p>
<p>（6） 向客户端发送数据。</p>
<p>（7） 关闭客户端Socket连接。</p>
<p>（8） 关闭服务端Socket连接。</p>
<p><strong>TCP服务器端接收数据</strong></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># coding=utf-8</span></span><br><span class="line"><span class="keyword">from</span> socket <span class="keyword">import</span> *</span><br><span class="line"></span><br><span class="line">server_socket = socket(AF_INET, SOCK_STREAM)  <span class="comment"># 创建TCP类型的套接字</span></span><br><span class="line">server_socket.bind(<span class="string">&quot;127.0.0.1&quot;</span>, <span class="number">8899</span>)  <span class="comment"># 本机监听8899端口</span></span><br><span class="line">server_socket.listen(<span class="number">5</span>)</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;等待接收连接！&quot;</span>)</span><br><span class="line">client_socket, client_info = server_socket.accept()</span><br><span class="line">recv_data = client_socket.recv(<span class="number">1024</span>)  <span class="comment"># 最大接收1024字节</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">f&quot;收到信息，<span class="subst">&#123;recv_data.decode(<span class="string">&#x27;gbk&#x27;</span>)&#125;</span>,来自:<span class="subst">&#123;client_info&#125;</span>&quot;</span>)</span><br><span class="line"></span><br><span class="line">client_socket.close()</span><br><span class="line">server_socket.close()</span><br></pre></td></tr></table></figure>
<p><strong>TCP客户端发送数据到服务端</strong></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># coding=utf-8</span></span><br><span class="line"><span class="keyword">from</span> socket <span class="keyword">import</span> *</span><br><span class="line"></span><br><span class="line">client_socket = socket(AF_INET, SOCK_STREAM)  <span class="comment"># 创建TCP类型的套接字</span></span><br><span class="line">client_socket.connect((<span class="string">&quot;127.0.0.1&quot;</span>, <span class="number">8899</span>))</span><br><span class="line">client_socket.send(<span class="string">&quot;hello&quot;</span>.encode(<span class="string">&#x27;gbk&#x27;</span>))</span><br><span class="line">client_socket.close()</span><br></pre></td></tr></table></figure>
<h4 id="TCP双向持续通信"><a href="#TCP双向持续通信" class="headerlink" title="TCP双向持续通信"></a>TCP双向持续通信</h4><p><strong>TCP：双向通信Socket之服务器端</strong></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># coding=utf-8</span></span><br><span class="line"><span class="keyword">from</span> socket <span class="keyword">import</span> *</span><br><span class="line"></span><br><span class="line">server_socket = socket(AF_INET, SOCK_STREAM)  <span class="comment"># 创建TCP类型的套接字</span></span><br><span class="line">server_socket.bind((<span class="string">&quot;127.0.0.1&quot;</span>, <span class="number">8899</span>))  <span class="comment"># 本机监听8899端口</span></span><br><span class="line">server_socket.listen(<span class="number">5</span>)</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;等待接收连接！&quot;</span>)</span><br><span class="line">client_socket, client_info = server_socket.accept()</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;一个客户端建立成功&quot;</span>)</span><br><span class="line"><span class="keyword">while</span> <span class="literal">True</span>:</span><br><span class="line">    recv_data = client_socket.recv(<span class="number">1024</span>)  <span class="comment"># 最大接收1024字节</span></span><br><span class="line">    recv_content = recv_data.decode(<span class="string">&#x27;gbk&#x27;</span>)</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">f&quot;客户端说：<span class="subst">&#123;recv_content&#125;</span>,来自:<span class="subst">&#123;client_info&#125;</span>&quot;</span>)</span><br><span class="line">    <span class="keyword">if</span> recv_content == <span class="string">&quot;end&quot;</span>:</span><br><span class="line">        <span class="keyword">break</span></span><br><span class="line">    msg = <span class="built_in">input</span>(<span class="string">&quot;&gt;&quot;</span>)</span><br><span class="line">    client_socket.send(msg.encode(<span class="string">&#x27;gbk&#x27;</span>))</span><br><span class="line"></span><br><span class="line">client_socket.close()</span><br><span class="line">server_socket.close()</span><br></pre></td></tr></table></figure>
<p><strong>TCP：双向通信Socke之客户端</strong></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># coding=utf-8</span></span><br><span class="line"><span class="keyword">from</span> socket <span class="keyword">import</span> *</span><br><span class="line"></span><br><span class="line">client_socket = socket(AF_INET, SOCK_STREAM)  <span class="comment"># 创建TCP类型的套接字</span></span><br><span class="line">client_socket.connect((<span class="string">&quot;127.0.0.1&quot;</span>, <span class="number">8899</span>))</span><br><span class="line"><span class="keyword">while</span> <span class="literal">True</span>:</span><br><span class="line">    <span class="comment"># 给服务端发消息</span></span><br><span class="line">    msg = <span class="built_in">input</span>(<span class="string">&quot;&gt;&quot;</span>)</span><br><span class="line">    client_socket.send(msg.encode(<span class="string">&#x27;gbk&#x27;</span>))</span><br><span class="line">    <span class="keyword">if</span> msg == <span class="string">&quot;end&quot;</span>:</span><br><span class="line">        <span class="keyword">break</span></span><br><span class="line">    <span class="comment"># 接收服务端消息</span></span><br><span class="line">    recv_data = client_socket.recv(<span class="number">1024</span>)  <span class="comment"># 最大接收1024字节</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">f&quot;服务端说：<span class="subst">&#123;recv_data.decode(<span class="string">&#x27;gbk&#x27;</span>)&#125;</span>&quot;</span>)</span><br><span class="line"></span><br><span class="line">client_socket.close()</span><br></pre></td></tr></table></figure>
<h4 id="结合多线程实现TCP双向传送-自由聊天"><a href="#结合多线程实现TCP双向传送-自由聊天" class="headerlink" title="结合多线程实现TCP双向传送(自由聊天)"></a>结合多线程实现TCP双向传送(自由聊天)</h4><p><strong>TCP服务端结合多线程实现自由收发信息</strong></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># coding=utf-8</span></span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br><span class="line"><span class="string">双向通信Socket之服务器端</span></span><br><span class="line"><span class="string">    读取客户端发送的数据，将内容输出到控制台</span></span><br><span class="line"><span class="string">    将控制台输入的信息发送给客户器端</span></span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br><span class="line"><span class="keyword">from</span> socket <span class="keyword">import</span> *</span><br><span class="line"><span class="keyword">from</span> threading <span class="keyword">import</span> Thread</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">recv_data</span>():</span><br><span class="line">    <span class="keyword">while</span> <span class="literal">True</span>:</span><br><span class="line">        <span class="comment"># 读取客户端的消息</span></span><br><span class="line">        recv_data = client_socket.recv(<span class="number">1024</span>)  <span class="comment"># 最大接收1024字节</span></span><br><span class="line">        recv_content = recv_data.decode(<span class="string">&#x27;gbk&#x27;</span>)</span><br><span class="line">        <span class="comment"># 将消息输出到控制台</span></span><br><span class="line">        <span class="built_in">print</span>(<span class="string">f&quot;客户端说：<span class="subst">&#123;recv_content&#125;</span>,来自:<span class="subst">&#123;client_info&#125;</span>&quot;</span>)</span><br><span class="line">        <span class="keyword">if</span> recv_content == <span class="string">&quot;end&quot;</span>:</span><br><span class="line">            <span class="built_in">print</span>(<span class="string">&quot;结束接收消息！&quot;</span>)</span><br><span class="line">            <span class="keyword">break</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">send_data</span>():</span><br><span class="line">    <span class="keyword">while</span> <span class="literal">True</span>:</span><br><span class="line">        <span class="comment"># 获取控制台信息</span></span><br><span class="line">        msg = <span class="built_in">input</span>(<span class="string">&quot;&gt;&quot;</span>)</span><br><span class="line">        client_socket.send(msg.encode(<span class="string">&#x27;gbk&#x27;</span>))</span><br><span class="line">        <span class="keyword">if</span> msg == <span class="string">&quot;end&quot;</span>:</span><br><span class="line">            <span class="built_in">print</span>(<span class="string">&quot;结束发送消息！&quot;</span>)</span><br><span class="line">            <span class="keyword">break</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&#x27;__main__&#x27;</span>:</span><br><span class="line">    <span class="comment"># 创建Socket对象</span></span><br><span class="line">    server_socket = socket(AF_INET, SOCK_STREAM)  <span class="comment"># 创建TCP类型的套接字</span></span><br><span class="line">    <span class="comment"># 绑定端口</span></span><br><span class="line">    server_socket.bind((<span class="string">&quot;127.0.0.1&quot;</span>, <span class="number">8899</span>))  <span class="comment"># 本机监听8899端口</span></span><br><span class="line">    <span class="comment"># 监听客户端的连接</span></span><br><span class="line">    server_socket.listen(<span class="number">5</span>)</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;等待接收连接！&quot;</span>)</span><br><span class="line">    <span class="comment"># 接收客户端连接</span></span><br><span class="line">    client_socket, client_info = server_socket.accept()</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;一个客户端建立成功&quot;</span>)</span><br><span class="line"></span><br><span class="line">    t1 = Thread(target=recv_data)</span><br><span class="line">    t2 = Thread(target=send_data)</span><br><span class="line">    t1.start()</span><br><span class="line">    t2.start()</span><br><span class="line">    t1.join()</span><br><span class="line">    t2.join()</span><br><span class="line"></span><br><span class="line">    client_socket.close()</span><br><span class="line">    server_socket.close()</span><br></pre></td></tr></table></figure>
<p><strong>TCP客户端结合多线程实现自由收发信息</strong></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># coding=utf-8</span></span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br><span class="line"><span class="string">双向通信Socket之客户端</span></span><br><span class="line"><span class="string">    将控制台输入的信息发送给服务器端</span></span><br><span class="line"><span class="string">    读取服务器端的数据，将内容输出到控制台</span></span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br><span class="line"><span class="keyword">from</span> socket <span class="keyword">import</span> *</span><br><span class="line"><span class="keyword">from</span> threading <span class="keyword">import</span> Thread</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">recv_data</span>():</span><br><span class="line">    <span class="keyword">while</span> <span class="literal">True</span>:</span><br><span class="line">        <span class="comment"># 接收服务端消息</span></span><br><span class="line">        recv_data = client_socket.recv(<span class="number">1024</span>)  <span class="comment"># 最大接收1024字节</span></span><br><span class="line">        recv_content = recv_data.decode(<span class="string">&#x27;gbk&#x27;</span>)</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">f&quot;服务端说：<span class="subst">&#123;recv_content&#125;</span>&quot;</span>)</span><br><span class="line">        <span class="keyword">if</span> recv_content == <span class="string">&quot;end&quot;</span>:</span><br><span class="line">            <span class="built_in">print</span>(<span class="string">&quot;结束接收消息！&quot;</span>)</span><br><span class="line">            <span class="keyword">break</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">send_data</span>():</span><br><span class="line">    <span class="keyword">while</span> <span class="literal">True</span>:</span><br><span class="line">        <span class="comment"># 给服务端发消息</span></span><br><span class="line">        msg = <span class="built_in">input</span>(<span class="string">&quot;&gt;&quot;</span>)</span><br><span class="line">        client_socket.send(msg.encode(<span class="string">&#x27;gbk&#x27;</span>))</span><br><span class="line">        <span class="keyword">if</span> msg == <span class="string">&quot;end&quot;</span>:</span><br><span class="line">            <span class="built_in">print</span>(<span class="string">&quot;结束发送消息！&quot;</span>)</span><br><span class="line">            <span class="keyword">break</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&#x27;__main__&#x27;</span>:</span><br><span class="line">    client_socket = socket(AF_INET, SOCK_STREAM)  <span class="comment"># 创建TCP类型的套接字</span></span><br><span class="line">    client_socket.connect((<span class="string">&quot;127.0.0.1&quot;</span>, <span class="number">8899</span>))</span><br><span class="line"></span><br><span class="line">    t1 = Thread(target=recv_data)</span><br><span class="line">    t2 = Thread(target=send_data)</span><br><span class="line">    t1.start()</span><br><span class="line">    t2.start()</span><br><span class="line">    t1.join()</span><br><span class="line">    t2.join()</span><br><span class="line"></span><br><span class="line">    client_socket.close()</span><br></pre></td></tr></table></figure>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="https://huaiyuechusan.github.io">SanShui</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://huaiyuechusan.github.io/archives/984515cf.html">https://huaiyuechusan.github.io/archives/984515cf.html</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="external nofollow noreferrer" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://huaiyuechusan.github.io" target="_blank">SanShui的个人博客</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/%E5%AD%A6%E4%B9%A0%E6%8A%80%E6%9C%AF/">学习技术</a><a class="post-meta__tags" href="/tags/Python/">Python</a></div><div class="post_share"><div class="social-share" data-image="http://wallpaper.csun.site/?6" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><div class="post-reward"><div class="reward-button"><i class="fas fa-qrcode"></i> 打赏</div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="/img/wechat.jpg" target="_blank"><img class="post-qr-code-img" src="/img/wechat.jpg" alt="wechat"/></a><div class="post-qr-code-desc">wechat</div></li><li class="reward-item"><a href="/img/alipay.jpg" target="_blank"><img class="post-qr-code-img" src="/img/alipay.jpg" alt="alipay"/></a><div class="post-qr-code-desc">alipay</div></li></ul></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/archives/8df5f8b9.html"><img class="prev-cover" src="http://wallpaper.csun.site/?5" onerror="onerror=null;src='/./img/config_img/%E5%A4%9C%E6%99%9A.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">Python深入和提高</div></div></a></div><div class="next-post pull-right"><a href="/archives/fab44cdf.html"><img class="next-cover" src="http://wallpaper.csun.site/?7" onerror="onerror=null;src='/./img/config_img/%E5%A4%9C%E6%99%9A.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">函数式编程和Python新核心特性</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/archives/fab44cdf.html" title="函数式编程和Python新核心特性"><img class="cover" src="http://wallpaper.csun.site/?7" alt="cover"><div class="content is-center"><div class="date"><i class="fas fa-history fa-fw"></i> 2024-10-17</div><div class="title">函数式编程和Python新核心特性</div></div></a></div><div><a href="/archives/8df5f8b9.html" title="Python深入和提高"><img class="cover" src="http://wallpaper.csun.site/?5" alt="cover"><div class="content is-center"><div class="date"><i class="fas fa-history fa-fw"></i> 2024-10-17</div><div class="title">Python深入和提高</div></div></a></div><div><a href="/archives/d9ef328.html" title="Python入门"><img class="cover" src="http://wallpaper.csun.site/?4" alt="cover"><div class="content is-center"><div class="date"><i class="fas fa-history fa-fw"></i> 2024-10-17</div><div class="title">Python入门</div></div></a></div><div><a href="/archives/6d169344.html" title="云函数调用request请求"><img class="cover" src="http://wallpaper.csun.site/?21" alt="cover"><div class="content is-center"><div class="date"><i class="fas fa-history fa-fw"></i> 2024-10-17</div><div class="title">云函数调用request请求</div></div></a></div><div><a href="/archives/d4b1abad.html" title="下载M3U8格式加密视频"><img class="cover" src="http://wallpaper.csun.site/?22" alt="cover"><div class="content is-center"><div class="date"><i class="fas fa-history fa-fw"></i> 2024-10-17</div><div class="title">下载M3U8格式加密视频</div></div></a></div><div><a href="/archives/59cb3162.html" title="大数据集群软件启动脚本"><img class="cover" src="http://wallpaper.csun.site/?11" alt="cover"><div class="content is-center"><div class="date"><i class="fas fa-history fa-fw"></i> 2024-10-17</div><div class="title">大数据集群软件启动脚本</div></div></a></div></div></div><hr/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i><span> 评论</span></div></div><div class="comment-wrap"><div><div id="lv-container" data-id="city" data-uid="MTAyMC81OTEzMS8zNTU5Mw=="></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/./img/config_img/%E9%98%B3%E5%85%89%E5%B0%8F%E7%8C%AB.jpg" onerror="this.onerror=null;this.src='/./img/config_img/%E8%93%9D%E5%A4%A9.jpg'" alt="avatar"/></div><div class="author-info__name">SanShui</div><div class="author-info__description">今天不学习，明天变垃圾</div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">25</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">16</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">11</div></a></div><a id="card-info-btn" target="_blank" rel="noopener external nofollow noreferrer" href="https://github.com/huaiyuechusan"><i class="fa-sharp fa-solid fa-plane"></i><span>欢迎关注我的Github</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/huaiyuechusan" rel="external nofollow noreferrer" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="https://huaiyuechusan.github.io/atom.xml" target="_blank" title=""><i class="fas fa-rss"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">欢迎来到SanShui的博客</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content is-expand"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#Python-%E7%BD%91%E7%BB%9C%E4%B8%8E%E5%B9%B6%E5%8F%91%E7%BC%96%E7%A8%8B"><span class="toc-text">Python 网络与并发编程</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%B9%B6%E5%8F%91%E7%BC%96%E7%A8%8B"><span class="toc-text">并发编程</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B9%B6%E5%8F%91%E7%BC%96%E7%A8%8B%E4%BB%8B%E7%BB%8D"><span class="toc-text">并发编程介绍</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%B8%B2%E8%A1%8C-%E5%B9%B6%E8%A1%8C-%E5%B9%B6%E5%8F%91%E7%9A%84%E5%8C%BA%E5%88%AB"><span class="toc-text">串行-并行-并发的区别</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%BF%9B%E7%A8%8B-%E7%BA%BF%E7%A8%8B-%E5%8D%8F%E7%A8%8B%E7%9A%84%E5%8C%BA%E5%88%AB"><span class="toc-text">进程-线程-协程的区别</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%90%8C%E6%AD%A5%E5%92%8C%E5%BC%82%E6%AD%A5%E9%80%9A%E4%BF%A1%E6%9C%BA%E5%88%B6%E7%9A%84%E5%8C%BA%E5%88%AB"><span class="toc-text">同步和异步通信机制的区别</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%BA%BF%E7%A8%8B"><span class="toc-text">线程</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%BA%BF%E7%A8%8B%E7%9A%84%E5%88%9B%E5%BB%BA%E6%96%B9%E5%BC%8F"><span class="toc-text">线程的创建方式</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%96%B9%E6%B3%95%E5%8C%85%E8%A3%85%E5%88%9B%E5%BB%BA%E7%BA%BF%E7%A8%8B"><span class="toc-text">方法包装创建线程</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E7%B1%BB%E5%8C%85%E8%A3%85%E5%88%9B%E5%BB%BA%E7%BA%BF%E7%A8%8B"><span class="toc-text">类包装创建线程</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#join-%E5%92%8C%E5%AE%88%E6%8A%A4%E7%BA%BF%E7%A8%8B"><span class="toc-text">join()和守护线程</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%85%A8%E5%B1%80%E8%A7%A3%E9%87%8A%E5%99%A8%E9%94%81GIL%E9%97%AE%E9%A2%98"><span class="toc-text">全局解释器锁GIL问题</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%BA%BF%E7%A8%8B%E5%90%8C%E6%AD%A5%E5%92%8C%E4%BA%92%E6%96%A5%E9%94%81%E8%B5%84%E6%BA%90%E5%86%B2%E7%AA%81%E6%A1%88%E4%BE%8B"><span class="toc-text">线程同步和互斥锁资源冲突案例</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%BA%92%E6%96%A5%E9%94%81%E5%85%B8%E5%9E%8B%E6%A1%88%E4%BE%8B"><span class="toc-text">互斥锁典型案例</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%AD%BB%E9%94%81%E9%97%AE%E9%A2%98%E5%92%8C%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88"><span class="toc-text">死锁问题和解决方案</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%BF%A1%E5%8F%B7%E9%87%8F"><span class="toc-text">信号量</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%BA%8B%E4%BB%B6Event%E5%AF%B9%E8%B1%A1"><span class="toc-text">事件Event对象</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%94%9F%E4%BA%A7%E8%80%85-%E6%B6%88%E8%B4%B9%E8%80%85%E6%A8%A1%E5%BC%8F"><span class="toc-text">生产者-消费者模式</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%BF%9B%E7%A8%8B"><span class="toc-text">进程</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%96%B9%E6%B3%95%E6%A8%A1%E5%BC%8F%E5%88%9B%E5%BB%BA%E8%BF%9B%E7%A8%8B"><span class="toc-text">方法模式创建进程</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#windows-%E5%A4%9A%E8%BF%9B%E7%A8%8B%E7%9A%84%E4%B8%80%E4%B8%AAbug"><span class="toc-text">windows:多进程的一个bug</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%B1%BB%E6%A8%A1%E5%BC%8F%E5%88%9B%E5%BB%BA%E8%BF%9B%E7%A8%8B"><span class="toc-text">类模式创建进程</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Queue%E5%AE%9E%E7%8E%B0%E8%BF%9B%E7%A8%8B%E9%80%9A%E4%BF%A1"><span class="toc-text">Queue实现进程通信</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Pipe%E7%AE%A1%E9%81%93%E5%AE%9E%E7%8E%B0%E8%BF%9B%E7%A8%8B%E9%80%9A%E4%BF%A1"><span class="toc-text">Pipe管道实现进程通信</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Manager%E7%AE%A1%E7%90%86%E5%99%A8%E5%AE%9E%E7%8E%B0%E8%BF%9B%E7%A8%8B%E9%80%9A%E4%BF%A1"><span class="toc-text">Manager管理器实现进程通信</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%BF%9B%E7%A8%8B%E6%B1%A0-Pool-%E7%AE%A1%E7%90%86%E8%BF%9B%E7%A8%8B%E7%9A%84%E4%B8%A4%E7%A7%8D%E5%85%B8%E5%9E%8B%E6%A1%88%E4%BE%8B"><span class="toc-text">进程池(Pool)管理进程的两种典型案例</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8D%8F%E7%A8%8B"><span class="toc-text">协程</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%A0%B8%E5%BF%83%E6%A6%82%E5%BF%B5%E9%9D%A2%E8%AF%95%E9%87%8D%E7%82%B9"><span class="toc-text">核心概念面试重点</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#yield%E6%96%B9%E5%BC%8F%E5%AE%9E%E7%8E%B0%EF%BC%88%E5%B7%B2%E6%B7%98%E6%B1%B0%EF%BC%8C%E4%BA%86%E8%A7%A3%E5%8D%B3%E5%8F%AF%EF%BC%89"><span class="toc-text">yield方式实现（已淘汰，了解即可）</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#asyncio%E5%BC%82%E6%AD%A5IO%E5%AE%9E%E7%8E%B0%E5%8D%8F%E7%A8%8B-%E9%87%8D%E7%82%B9%EF%BC%89"><span class="toc-text">asyncio异步IO实现协程(重点）</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%BD%91%E7%BB%9C%E9%80%9A%E4%BF%A1"><span class="toc-text">网络通信</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5"><span class="toc-text">基本概念</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#IP%E5%9C%B0%E5%9D%80"><span class="toc-text">IP地址</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%AB%AF%E5%8F%A3port"><span class="toc-text">端口port</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%BD%91%E7%BB%9C%E9%80%9A%E4%BF%A1%E5%8D%8F%E8%AE%AE"><span class="toc-text">网络通信协议</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%BD%91%E7%BB%9C%E9%80%9A%E4%BF%A1%E5%8D%8F%E8%AE%AE%E7%9A%84%E5%88%86%E5%B1%82-ISO%E5%88%86%E5%B1%82%E6%A8%A1%E5%9E%8B"><span class="toc-text">网络通信协议的分层-ISO分层模型</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#TCP%E5%92%8CUDP%E4%BC%A0%E8%BE%93%E6%95%B0%E6%8D%AE%E7%9A%84%E5%8C%BA%E5%88%AB"><span class="toc-text">TCP和UDP传输数据的区别</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#TCP%E5%BB%BA%E7%AB%8B%E8%BF%9E%E6%8E%A5%E7%9A%84%E4%B8%89%E6%AC%A1%E6%8F%A1%E6%89%8B"><span class="toc-text">TCP建立连接的三次握手</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#TCP%E6%96%AD%E5%BC%80%E8%BF%9E%E6%8E%A5%E7%9A%84%E5%9B%9B%E6%AC%A1%E6%8C%A5%E6%89%8B"><span class="toc-text">TCP断开连接的四次挥手</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%95%B0%E6%8D%AE%E5%8C%85%E4%B8%8E%E5%A4%84%E7%90%86%E6%B5%81%E7%A8%8B"><span class="toc-text">数据包与处理流程</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%A5%97%E6%8E%A5%E5%AD%97%E7%BC%96%E7%A8%8B%E5%AE%9E%E6%88%98"><span class="toc-text">套接字编程实战</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#socket%E7%BC%96%E7%A8%8B%E4%BB%8B%E7%BB%8D"><span class="toc-text">socket编程介绍</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#socket-%E5%87%BD%E6%95%B0%E4%BB%8B%E7%BB%8D"><span class="toc-text">socket()函数介绍</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#socket%E5%AF%B9%E8%B1%A1%E7%9A%84%E5%86%85%E7%BD%AE%E5%87%BD%E6%95%B0%E5%92%8C%E5%B1%9E%E6%80%A7"><span class="toc-text">socket对象的内置函数和属性</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#UDP%E7%BC%96%E7%A8%8B%E4%BB%8B%E7%BB%8D"><span class="toc-text">UDP编程介绍</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#UDP%E7%BC%96%E7%A8%8B%E7%9A%84%E5%AE%9E%E7%8E%B0"><span class="toc-text">UDP编程的实现</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#UDP%E6%8C%81%E7%BB%AD%E9%80%9A%E4%BF%A1"><span class="toc-text">UDP持续通信</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%BB%93%E5%90%88%E5%A4%9A%E7%BA%BF%E7%A8%8B%E5%AE%9E%E7%8E%B0UDP%E5%8F%8C%E5%90%91%E8%87%AA%E7%94%B1%E9%80%9A%E4%BF%A1"><span class="toc-text">结合多线程实现UDP双向自由通信</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#TCP%E7%BC%96%E7%A8%8B%E4%BB%8B%E7%BB%8D"><span class="toc-text">TCP编程介绍</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#TCP%E7%BC%96%E7%A8%8B%E7%9A%84%E5%AE%9E%E7%8E%B0"><span class="toc-text">TCP编程的实现</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#TCP%E5%8F%8C%E5%90%91%E6%8C%81%E7%BB%AD%E9%80%9A%E4%BF%A1"><span class="toc-text">TCP双向持续通信</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%BB%93%E5%90%88%E5%A4%9A%E7%BA%BF%E7%A8%8B%E5%AE%9E%E7%8E%B0TCP%E5%8F%8C%E5%90%91%E4%BC%A0%E9%80%81-%E8%87%AA%E7%94%B1%E8%81%8A%E5%A4%A9"><span class="toc-text">结合多线程实现TCP双向传送(自由聊天)</span></a></li></ol></li></ol></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/archives/6ca065c5.html" title="SanShui API 使用教程"><img src="http://wallpaper.csun.site/?abc" onerror="this.onerror=null;this.src='/./img/config_img/%E5%A4%9C%E6%99%9A.jpg'" alt="SanShui API 使用教程"/></a><div class="content"><a class="title" href="/archives/6ca065c5.html" title="SanShui API 使用教程">SanShui API 使用教程</a><time datetime="2025-03-13T13:21:12.311Z" title="更新于 2025-03-13 21:21:12">2025-03-13</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/archives/b5700173.html" title="微信小程序定时订阅消息问题"><img src="http://wallpaper.csun.site/?25" onerror="this.onerror=null;this.src='/./img/config_img/%E5%A4%9C%E6%99%9A.jpg'" alt="微信小程序定时订阅消息问题"/></a><div class="content"><a class="title" href="/archives/b5700173.html" title="微信小程序定时订阅消息问题">微信小程序定时订阅消息问题</a><time datetime="2024-10-17T12:04:53.379Z" title="更新于 2024-10-17 20:04:53">2024-10-17</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/archives/638889b7.html" title="深度学习回归任务训练代码模版"><img src="http://wallpaper.csun.site/?24" onerror="this.onerror=null;this.src='/./img/config_img/%E5%A4%9C%E6%99%9A.jpg'" alt="深度学习回归任务训练代码模版"/></a><div class="content"><a class="title" href="/archives/638889b7.html" title="深度学习回归任务训练代码模版">深度学习回归任务训练代码模版</a><time datetime="2024-10-17T12:04:09.066Z" title="更新于 2024-10-17 20:04:09">2024-10-17</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/archives/7cb9f0a8.html" title="通过nginx访问tomcat中SpringMVC应用"><img src="http://wallpaper.csun.site/?23" onerror="this.onerror=null;this.src='/./img/config_img/%E5%A4%9C%E6%99%9A.jpg'" alt="通过nginx访问tomcat中SpringMVC应用"/></a><div class="content"><a class="title" href="/archives/7cb9f0a8.html" title="通过nginx访问tomcat中SpringMVC应用">通过nginx访问tomcat中SpringMVC应用</a><time datetime="2024-10-17T12:02:23.321Z" title="更新于 2024-10-17 20:02:23">2024-10-17</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/archives/d4b1abad.html" title="下载M3U8格式加密视频"><img src="http://wallpaper.csun.site/?22" onerror="this.onerror=null;this.src='/./img/config_img/%E5%A4%9C%E6%99%9A.jpg'" alt="下载M3U8格式加密视频"/></a><div class="content"><a class="title" href="/archives/d4b1abad.html" title="下载M3U8格式加密视频">下载M3U8格式加密视频</a><time datetime="2024-10-17T12:02:02.720Z" title="更新于 2024-10-17 20:02:02">2024-10-17</time></div></div></div></div></div></div></main><footer id="footer" style="background-image: url('http://wallpaper.csun.site/?6')"><div id="footer-wrap"><div class="footer_custom_text">更多内容查看<a target="_blank" rel="noopener external nofollow noreferrer" href="https://github.com/huaiyuechusan/">我的GitHub</a> ！</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">繁</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button><button id="go-down" type="button" title="直达底部" onclick="btf.scrollToDest(document.body.scrollHeight, 500)"><i class="fas fa-arrow-down"></i></button></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="/js/tw_cn.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.min.js"></script><script src="https://cdn.jsdelivr.net/npm/instant.page/instantpage.min.js" type="module"></script><script src="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.js"></script><script>function panguFn () {
  if (typeof pangu === 'object') pangu.autoSpacingPage()
  else {
    getScript('https://cdn.jsdelivr.net/npm/pangu/dist/browser/pangu.min.js')
      .then(() => {
        pangu.autoSpacingPage()
      })
  }
}

function panguInit () {
  if (false){
    GLOBAL_CONFIG_SITE.isPost && panguFn()
  } else {
    panguFn()
  }
}

document.addEventListener('DOMContentLoaded', panguInit)</script><script src="/js/search/local-search.js"></script><div class="js-pjax"><script>if (!window.MathJax) {
  window.MathJax = {
    tex: {
      inlineMath: [ ['$','$'], ["\\(","\\)"]],
      tags: 'ams'
    },
    chtml: {
      scale: 1.1
    },
    options: {
      renderActions: {
        findScript: [10, doc => {
          for (const node of document.querySelectorAll('script[type^="math/tex"]')) {
            const display = !!node.type.match(/; *mode=display/)
            const math = new doc.options.MathItem(node.textContent, doc.inputJax[0], display)
            const text = document.createTextNode('')
            node.parentNode.replaceChild(text, node)
            math.start = {node: text, delim: '', n: 0}
            math.end = {node: text, delim: '', n: 0}
            doc.math.push(math)
          }
        }, ''],
        insertScript: [200, () => {
          document.querySelectorAll('mjx-container').forEach(node => {
            if (node.hasAttribute('display')) {
              btf.wrap(node, 'div', { class: 'mathjax-overflow' })
            } else {
              btf.wrap(node, 'span', { class: 'mathjax-overflow' })
            }
          });
        }, '', false]
      }
    }
  }
  
  const script = document.createElement('script')
  script.src = 'https://cdn.jsdelivr.net/npm/mathjax/es5/tex-mml-chtml.min.js'
  script.id = 'MathJax-script'
  script.async = true
  document.head.appendChild(script)
} else {
  MathJax.startup.document.state(0)
  MathJax.texReset()
  MathJax.typeset()
}</script><script>function loadLivere () {
  if (typeof LivereTower === 'object') {
    window.LivereTower.init()
  }
  else {
    (function(d, s) {
        var j, e = d.getElementsByTagName(s)[0];
        if (typeof LivereTower === 'function') { return; }
        j = d.createElement(s);
        j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
        j.async = true;
        e.parentNode.insertBefore(j, e);
    })(document, 'script');
  }
}

if ('Livere' === 'Livere' || !true) {
  if (true) btf.loadComment(document.getElementById('lv-container'), loadLivere)
  else loadLivere()
}
else {
  function loadOtherComment () {
    loadLivere()
  }
}</script></div><div class="aplayer no-destroy" data-id="875151895" data-server="netease" data-type="playlist" data-fixed="true" data-autoplay="true" data-volume="0.5"> </div><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/activate-power-mode.min.js"></script><script>POWERMODE.colorful = true;
POWERMODE.shake = false;
POWERMODE.mobile = false;
document.body.addEventListener('input', POWERMODE);
</script><script id="click-show-text" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/click-show-text.min.js" data-mobile="false" data-text="Nice,To,Meet,YOU" data-fontsize="15px" data-random="false" async="async"></script><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.js"></script><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/metingjs/dist/Meting.min.js"></script><script src="https://cdn.jsdelivr.net/npm/pjax/pjax.min.js"></script><script>let pjaxSelectors = ["meta[property=\"og:image\"]","meta[property=\"og:title\"]","meta[property=\"og:url\"]","head > title","#config-diff","#body-wrap","#rightside-config-hide","#rightside-config-show",".js-pjax"]

var pjax = new Pjax({
  elements: 'a:not([target="_blank"])',
  selectors: pjaxSelectors,
  cacheBust: false,
  analytics: false,
  scrollRestoration: false
})

document.addEventListener('pjax:send', function () {

  // removeEventListener scroll 
  window.tocScrollFn && window.removeEventListener('scroll', window.tocScrollFn)
  window.scrollCollect && window.removeEventListener('scroll', scrollCollect)

  document.getElementById('rightside').style.cssText = "opacity: ''; transform: ''"
  
  if (window.aplayers) {
    for (let i = 0; i < window.aplayers.length; i++) {
      if (!window.aplayers[i].options.fixed) {
        window.aplayers[i].destroy()
      }
    }
  }

  typeof typed === 'object' && typed.destroy()

  //reset readmode
  const $bodyClassList = document.body.classList
  $bodyClassList.contains('read-mode') && $bodyClassList.remove('read-mode')

  typeof disqusjs === 'object' && disqusjs.destroy()
})

document.addEventListener('pjax:complete', function () {
  window.refreshFn()

  document.querySelectorAll('script[data-pjax]').forEach(item => {
    const newScript = document.createElement('script')
    const content = item.text || item.textContent || item.innerHTML || ""
    Array.from(item.attributes).forEach(attr => newScript.setAttribute(attr.name, attr.value))
    newScript.appendChild(document.createTextNode(content))
    item.parentNode.replaceChild(newScript, item)
  })

  GLOBAL_CONFIG.islazyload && window.lazyLoadInstance.update()

  typeof chatBtnFn === 'function' && chatBtnFn()
  typeof panguInit === 'function' && panguInit()

  // google analytics
  typeof gtag === 'function' && gtag('config', '', {'page_path': window.location.pathname});

  // baidu analytics
  typeof _hmt === 'object' && _hmt.push(['_trackPageview',window.location.pathname]);

  typeof loadMeting === 'function' && document.getElementsByClassName('aplayer').length && loadMeting()

  // prismjs
  typeof Prism === 'object' && Prism.highlightAll()
})

document.addEventListener('pjax:error', (e) => {
  if (e.request.status === 404) {
    pjax.loadUrl('/404.html')
  }
})</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><!-- hexo injector body_end start --><script data-pjax>
    function butterfly_categories_card_injector_config(){
      var parent_div_git = document.getElementById('recent-posts');
      var item_html = '<style>li.categoryBar-list-item{width:32.3%;}.categoryBar-list{max-height: 380px;overflow:auto;}.categoryBar-list::-webkit-scrollbar{width:0!important}@media screen and (max-width: 650px){.categoryBar-list{max-height: 320px;}}</style><div class="recent-post-item" style="height:auto;width:100%;padding:0px;"><div id="categoryBar"><ul class="categoryBar-list"><li class="categoryBar-list-item" style="background:url(./img/config_img/image-20231112202739060-2023-11-1220-27-56.png);"> <a class="categoryBar-list-link" onclick="pjax.loadUrl(&quot;categories/AI大模型/&quot;);" href="javascript:void(0);" rel="external nofollow noreferrer">AI大模型</a><span class="categoryBar-list-count">3</span><span class="categoryBar-list-descr"></span></li><li class="categoryBar-list-item" style="background:url(./img/config_img/202310300007012-2023-11-1123-30-55.jpg);"> <a class="categoryBar-list-link" onclick="pjax.loadUrl(&quot;categories/微信小程序/&quot;);" href="javascript:void(0);" rel="external nofollow noreferrer">微信小程序</a><span class="categoryBar-list-count">2</span><span class="categoryBar-list-descr"></span></li><li class="categoryBar-list-item" style="background:url(./img/config_img/Girl-2023-11-2221_46_26.png);"> <a class="categoryBar-list-link" onclick="pjax.loadUrl(&quot;categories/爬虫/&quot;);" href="javascript:void(0);" rel="external nofollow noreferrer">爬虫</a><span class="categoryBar-list-count">1</span><span class="categoryBar-list-descr"></span></li><li class="categoryBar-list-item" style="background:url(./img/config_img/202310300007316-2023-11-1123-30-57.png);"> <a class="categoryBar-list-link" onclick="pjax.loadUrl(&quot;categories/深度学习/&quot;);" href="javascript:void(0);" rel="external nofollow noreferrer">深度学习</a><span class="categoryBar-list-count">4</span><span class="categoryBar-list-descr"></span></li><li class="categoryBar-list-item" style="background:url(./img/config_img/Cat-2023-11-2221-47-16.png);"> <a class="categoryBar-list-link" onclick="pjax.loadUrl(&quot;categories/大数据/&quot;);" href="javascript:void(0);" rel="external nofollow noreferrer">大数据</a><span class="categoryBar-list-count">1</span><span class="categoryBar-list-descr"></span></li><li class="categoryBar-list-item" style="background:url(./img/config_img/preview-2024-1-8-17-36-46.jpg);"> <a class="categoryBar-list-link" onclick="pjax.loadUrl(&quot;categories/PyTorch/&quot;);" href="javascript:void(0);" rel="external nofollow noreferrer">PyTorch</a><span class="categoryBar-list-count">3</span><span class="categoryBar-list-descr"></span></li><li class="categoryBar-list-item" style="background:url(./img/config_img/preview-2024-1-8-17-36-11.jpg);"> <a class="categoryBar-list-link" onclick="pjax.loadUrl(&quot;categories/多模态推荐系统/&quot;);" href="javascript:void(0);" rel="external nofollow noreferrer">多模态推荐系统</a><span class="categoryBar-list-count">1</span><span class="categoryBar-list-descr"></span></li><li class="categoryBar-list-item" style="background:url(./img/config_img/Starry-Night-in-Anime-Wallpaper-2023-11-2221-46-39.png);"> <a class="categoryBar-list-link" onclick="pjax.loadUrl(&quot;categories/学习总结/&quot;);" href="javascript:void(0);" rel="external nofollow noreferrer">学习总结</a><span class="categoryBar-list-count">2</span><span class="categoryBar-list-descr"></span></li><li class="categoryBar-list-item" style="background:url(./img/page_img/wallhaven-y8lqo7.jpg);"> <a class="categoryBar-list-link" onclick="pjax.loadUrl(&quot;categories/竞赛/&quot;);" href="javascript:void(0);" rel="external nofollow noreferrer">竞赛</a><span class="categoryBar-list-count">2</span><span class="categoryBar-list-descr"></span></li><li class="categoryBar-list-item" style="background:url(./img/config_img/preview-2024-1-8-17-34-25.jpg);"> <a class="categoryBar-list-link" onclick="pjax.loadUrl(&quot;categories/Python/&quot;);" href="javascript:void(0);" rel="external nofollow noreferrer">Python</a><span class="categoryBar-list-count">4</span><span class="categoryBar-list-descr"></span></li><li class="categoryBar-list-item" style="background:url(./img/config_img/preview-2024-1-8-17-37-57.jpg);"> <a class="categoryBar-list-link" onclick="pjax.loadUrl(&quot;categories/Liunx/&quot;);" href="javascript:void(0);" rel="external nofollow noreferrer">Liunx</a><span class="categoryBar-list-count">2</span><span class="categoryBar-list-descr"></span></li></ul></div></div>';
      console.log('已挂载butterfly_categories_card')
      parent_div_git.insertAdjacentHTML("afterbegin",item_html)
      }
    if( document.getElementById('recent-posts') && (location.pathname ==='/'|| '/' ==='all')){
    butterfly_categories_card_injector_config()
    }
  </script><script data-pjax>
  function butterfly_footer_beautify_injector_config(){
    var parent_div_git = document.getElementById('footer-wrap');
    var item_html = '<div id="workboard"></div><p id="ghbdages"><a class="github-badge" target="_blank" href="https://hexo.io/" rel="external nofollow noreferrer" style="margin-inline:5px" data-title="博客框架为Hexo_v6.2.0" title=""><img src="https://img.shields.io/badge/Frame-Hexo-blue?style=flat&amp;logo=hexo" alt=""/></a><a class="github-badge" target="_blank" href="https://butterfly.js.org/" rel="external nofollow noreferrer" style="margin-inline:5px" data-title="主题版本Butterfly_v4.3.1" title=""><img src="https://img.shields.io/badge/Theme-Butterfly-6513df?style=flat&amp;logo=bitdefender" alt=""/></a><a class="github-badge" target="_blank" href="https://github.com/" rel="external nofollow noreferrer" style="margin-inline:5px" data-title="本站采用多线部署，主线路托管于Github Pages" title=""><img src="https://img.shields.io/badge/Hosted-Github Pages-brightgreen?style=flat&amp;logo=Github" alt=""/></a><a class="github-badge" target="_blank" href="https://gitee.com/" rel="external nofollow noreferrer" style="margin-inline:5px" data-title="本站采用多线部署，备用线路托管于Gitee Pages" title=""><img src="https://img.shields.io/badge/Hosted-Gitee Pages-22DDDD?style=flat&amp;logo=Gitee" alt=""/></a><a class="github-badge" target="_blank" href="https://github.com/" rel="external nofollow noreferrer" style="margin-inline:5px" data-title="本站项目由Github托管" title=""><img src="https://img.shields.io/badge/Source-Github-d021d6?style=flat&amp;logo=GitHub" alt=""/></a><a class="github-badge" target="_blank" href="http://creativecommons.org/licenses/by-nc-sa/4.0/" rel="external nofollow noreferrer" style="margin-inline:5px" data-title="本站采用知识共享署名-非商业性使用-相同方式共享4.0国际许可协议进行许可" title=""><img src="https://img.shields.io/badge/Copyright-BY--NC--SA%204.0-d42328?style=flat&amp;logo=Claris" alt=""/></a></p>';
    console.log('已挂载butterfly_footer_beautify')
    parent_div_git.insertAdjacentHTML("beforeend",item_html)
    }
  var elist = 'null'.split(',');
  var cpage = location.pathname;
  var epage = 'all';
  var flag = 0;

  for (var i=0;i<elist.length;i++){
    if (cpage.includes(elist[i])){
      flag++;
    }
  }

  if ((epage ==='all')&&(flag == 0)){
    butterfly_footer_beautify_injector_config();
  }
  else if (epage === cpage){
    butterfly_footer_beautify_injector_config();
  }
  </script><script async src="/./js/runtime.js"></script><!-- hexo injector body_end end --></body></html>